62,046
社区成员
发帖
与我相关
我的任务
分享
private void SendMailByNet()
{
mailMessage = new MailMessage() ;
////设置收信人地址
mailMessage.To.Add(txt_To.Text);
//设置发信人
mailMessage.From = new System.Net.Mail.MailAddress("253631203@qq.com");
//发信主题
mailMessage.Subject = txt_Subject.Text;
//发信内容
mailMessage.IsBodyHtml = true;
mailMessage.Body = txt_Msg.Text;
//设置此电子邮件的优先级
mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
//设置编码格式
mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
//发送邮件到smpt服务器上
smptClient = new SmtpClient("smtp.qq.com");
smptClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, "z456123");
smptClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smptClient.Send(mailMessage);
}
MailMessage mail = new MailMessage();
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "253631203@qq.com");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "z456123");
mail.BodyFormat = MailFormat.Text;
mail.BodyEncoding = Encoding.UTF8;
mail.From = "253631203@qq.com";
mail.To = txt_To.Text;
mail.Subject = txt_Subject.Text;
mail.Body = txt_Msg.Text ;
SmtpMail.SmtpServer = "smtp.qq.com";
SmtpMail.Send(mail);
我这样写对吗?using System;
using System.Web.Mail;
namespace SendMail
{
class usage
{
public void DisplayUsage()
{
Console.WriteLine("Usage SendMail.exe <to> <from> <subject> <body>");
Console.WriteLine("<to> the addresses of the email recipients");
Console.WriteLine("<from> your email address");
Console.WriteLine("<subject> subject of your email");
Console.WriteLine("<body> the text of the email");
Console.WriteLine("Example:");
Console.WriteLine("SendMail.exe SomeOne@Contoso.com;SomeOther@Contoso.com Me@contoso.com Hi hello");
}
}
class Start
{
// The main entry point for the application.
[STAThread]
static void Main(string[] args)
{
try
{
try
{
MailMessage Message = new MailMessage();
Message.To = args[0];
Message.From = args[1];
Message.Subject = args[2];
Message.Body = args[3];
try
{
SmtpMail.SmtpServer = "your mail server name goes here";
SmtpMail.Send(Message);
}
catch(System.Web.HttpException ehttp)
{
Console.WriteLine("{0}", ehttp.Message);
Console.WriteLine("Here is the full error message output");
Console.Write("{0}", ehttp.ToString());
}
}
catch(IndexOutOfRangeException)
{
usage use = new usage();
use.DisplayUsage();
}
}
catch(System.Exception e)
{
Console.WriteLine("Unknown Exception occurred {0}", e.Message);
Console.WriteLine("Here is the Full Message output");
Console.WriteLine("{0}", e.ToString());
}
}
}
}
发送文本邮件(已测试可用)
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.Mail.MailMessage mail=new MailMessage();
mail.To="123@yahoo.com.cn";
mail.From="456@126.com";
mail.Subject="wwww";
//mail.BodyFormat=System.Web.Mail.MailFormat.Text;
mail.BodyFormat=System.Web.Mail.MailFormat.Html;
//string mes=""+Request["mailto"]+",您好。"+"您的朋友:"+Request["username"]+"从
信息网给您推荐了一篇文章--"+Request["url"]+"请您浏览!
";
mail.Body="mes";
//mail.Body=this.message.Text.ToString();
//SmtpMail.SmtpServer= "smtp.163.com";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "123456"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456"); //set your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/languagecode","0x0804");
SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here
SmtpMail.Send(mail);
}