您的位置:首页 > 大数据 > 人工智能

利用System.Net.Mail 的SmtpClient发送邮件

2013-03-18 16:55 661 查看
几个月前总结过关于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

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: