利用System.Net.Mail 的SmtpClient发送邮件
几个月前总结过关于Jmail发送邮件,当时用Jmail发送邮件发送速度有点慢(可能对Jmail了解不是很多).现在改为用微软提供的SmtpClient来发送邮件。
MailMessage 用于构造可以使用SmtpClient类传输到Smtp服务器以便传递的电子邮件;
使用MailMessage初始化MailMessage对象时,可以将电子邮件的发信人,收件人,主题和正文指定为参数。这些参数也可能被设置,或者使用MailMessage对象上的属性访问。
常用的属性:
附件 Attachments
密送 bcc
抄送 cc
Content-Type bodyEncoding/subjectEncoding
邮件正文 body
收件人 To
发件人 From
Subject sujbect
使用AlternateViews属性指定一个电子邮件不同格式的副本,如果发送HTML格式的邮件,可能希望同时提供邮件的纯文本格式,以防止一些收件人使用的电子邮件阅读程序无法显示html内容,另外这是我们使用body属性指定文本格式。
下面用两种方式发邮件:
Send
1 ArrayList list = new ArrayList(); 2 list.Add("*****@neotrident.com"); 3 list.Add("*****@gmail.com"); 4 list.Add("*****@126.com"); 5 list.Add("*****@qq.com"); 6 list.Add("*****@yahoo.com.cn"); 7 list.Add("*****@tarena.com.cn"); 8 list.Add("*****@me.com"); 9 MailMessage mailMessage = new MailMessage(); 10 for (int i = 0; i < list.Count; i++) 11 { 12 MailAddress toAddress = new MailAddress(list[i].ToString()); 13 mailMessage.To.Add(toAddress);//收件人 14 mailMessage.CC.Add(toAddress);//抄送 15 mailMessage.Bcc.Add(toAddress);//密送 16 } 17 string fileName = @"E:\2012年Q2新员工培训.pdf"; 18 Attachment attachment = new Attachment(fileName); 19 20 mailMessage.Attachments.Add(attachment); 21 mailMessage.From = new MailAddress("发件人邮箱地址", "**科技有限公司"); 22 mailMessage.Subject = "hello"; 23 mailMessage.SubjectEncoding = System.Text.Encoding.Unicode; 24 mailMessage.Body = "hello world"; 25 mailMessage.BodyEncoding = System.Text.Encoding.Unicode; 26 string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"; 27 body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">"; 28 body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text"; 29 body += "</FONT></DIV></BODY></HTML>"; 30 ContentType type = new ContentType("text/html"); 31 AlternateView view = AlternateView.CreateAlternateViewFromString(body, Encoding.Unicode, "text/html"); 32 mailMessage.AlternateViews.Add(view); 33 SmtpClient client = new SmtpClient("smtp.126.com"); 34 client.Credentials = new System.Net.NetworkCredential("发件人邮箱地址", "邮箱密码"); 35 client.Send(mailMessage);
第二种利用:SendAsync,
将指定的电子邮件发送到 SMTP 服务器以便传递。 此方法不会阻止调用线程,并允许调用方将对象传递给操作完成时调用的方法。第二种方式要在页面中设置Async="true"
SendAsync
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 ArrayList list = new ArrayList(); 4 list.Add("***@neotrident.com"); 5 list.Add("***@gmail.com"); 6 list.Add("***@126.com"); 7 list.Add("***@qq.com"); 8 list.Add("***@yahoo.com.cn"); 9 list.Add("***@tarena.com.cn"); 10 list.Add("***@me.com"); 11 MailMessage mailMessage = new MailMessage(); 12 for (int i = 0; i < list.Count; i++) 13 { 14 MailAddress toAddress = new MailAddress(list[i].ToString()); 15 mailMessage.To.Add(toAddress);//收件人 16 mailMessage.CC.Add(toAddress);//抄送 17 mailMessage.Bcc.Add(toAddress);//密送 18 } 19 string fileName = @"E:\2012年Q2新员工培训.pdf"; 20 Attachment attachment = new Attachment(fileName); 21 22 mailMessage.Attachments.Add(attachment); 23 mailMessage.From = new MailAddress("发件人地址", "**科技有限公司"); 24 mailMessage.Subject = "hello"; 25 mailMessage.SubjectEncoding = System.Text.Encoding.Unicode; 26 mailMessage.Body = "hello world"; 27 mailMessage.BodyEncoding = System.Text.Encoding.Unicode; 28 string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"; 29 body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">"; 30 body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text"; 31 body += "</FONT></DIV></BODY></HTML>"; 32 ContentType type = new ContentType("text/html"); 33 AlternateView view = AlternateView.CreateAlternateViewFromString(body, Encoding.Unicode, "text/html"); 34 mailMessage.AlternateViews.Add(view); 35 SmtpClient client = new SmtpClient("smtp.126.com"); 36 client.Credentials = new System.Net.NetworkCredential("发件人地址", "密码"); 37 //client.Send(mailMessage); 38 client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted); 39 string userState = "test message1"; 40 client.SendAsync(mailMessage, userState); 41 if (mailsent == false) 42 { 43 client.SendAsyncCancel(); 44 } 45 Response.Write("Goodbye"); 46 47 } 48 static bool mailsent = false; 49 void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 50 { 51 string token = (string)e.UserState; 52 if (e.Cancelled) 53 { 54 Response.Write(string.Format("{0} Send canceled.",token)); 55 } 56 if (e.Error != null) 57 { 58 Response.Write(string.Format("[{0}] {1}", token, e.Error)); 59 } 60 else 61 { 62 Response.Write("Message sent"); 63 } 64 mailsent = true; 65 }
就这可以用2种方式,以上代码包括发送附件,to,bcc,cc,正文内容的不同格式(html)