C#发送Email邮件方法总结 (转载)
2011-06-01 16:19 假面Wilson 阅读(292) 评论(0) 编辑 收藏 举报通过.Net FrameWork 2.0下提供的“System.Net.Mail”可以轻松的实现,本文列举了3种途径来发送:
1.通过Localhost;
2.通过普通SMTP;
3.通过SSL的SMTP;
下面一个一个来说:
1.通过LocalHost
1
public void SendMailLocalhost()
2
{
3
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
4
msg.To.Add("a@a.com");
5
msg.To.Add("b@b.com");
6
/*
7
* msg.To.Add("b@b.com");
8
* msg.To.Add("b@b.com");
9
* msg.To.Add("b@b.com");可以发送给多人
10
*/
11
msg.CC.Add("c@c.com");
12
/*
13
* msg.CC.Add("c@c.com");
14
* msg.CC.Add("c@c.com");可以抄送给多人
15
*/
16
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8);
17
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
18
msg.Subject = "这是测试邮件";//邮件标题
19
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
20
msg.Body = "邮件内容";//邮件内容
21
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
22
msg.IsBodyHtml = false;//是否是HTML邮件
23
msg.Priority = MailPriority.High;//邮件优先级
24
25
SmtpClient client = new SmtpClient();
26
client.Host = "localhost";
27
object userState = msg;
28
try
29
{
30
client.SendAsync(msg, userState);
31
//简单一点儿可以client.Send(msg);
32
MessageBox.Show("发送成功");
33
}
34
catch (System.Net.Mail.SmtpException ex)
35
{
36
MessageBox.Show(ex.Message, "发送邮件出错");
37
}
38
}
39![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
2
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
39
![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
2.通过普通SMTP
1
public void SendMailUseZj()
2
{
3
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
4
msg.To.Add("a@a.com");
5
msg.To.Add("b@b.com");
6
/*
7
* msg.To.Add("b@b.com");
8
* msg.To.Add("b@b.com");
9
* msg.To.Add("b@b.com");可以发送给多人
10
*/
11
msg.CC.Add("c@c.com");
12
/*
13
* msg.CC.Add("c@c.com");
14
* msg.CC.Add("c@c.com");可以抄送给多人
15
*/
16
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8);
17
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
18
msg.Subject = "这是测试邮件";//邮件标题
19
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
20
msg.Body = "邮件内容";//邮件内容
21
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
22
msg.IsBodyHtml = false;//是否是HTML邮件
23
msg.Priority = MailPriority.High;//邮件优先级
24
25
SmtpClient client = new SmtpClient();
26
client.Credentials = new System.Net.NetworkCredential("username@zj.com", "userpass");
27
//在zj.com注册的邮箱和密码
28
client.Host = "smtp.zj.com";
29
object userState = msg;
30
try
31
{
32
client.SendAsync(msg, userState);
33
//简单一点儿可以client.Send(msg);
34
MessageBox.Show("发送成功");
35
}
36
catch (System.Net.Mail.SmtpException ex)
37
{
38
MessageBox.Show(ex.Message, "发送邮件出错");
39
}
40
}
41![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
2
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
34
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
41
![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
上述方法不适用于所有SMTP,经测试zj.com可以,而smtp.163.com不行
3.通过SSL的SMTP
1
public void SendMailUseGmail()
2
{
3
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
4
msg.To.Add("a@a.com");
5
msg.To.Add("b@b.com");
6
/*
7
* msg.To.Add("b@b.com");
8
* msg.To.Add("b@b.com");
9
* msg.To.Add("b@b.com");可以发送给多人
10
*/
11
msg.CC.Add("c@c.com");
12
/*
13
* msg.CC.Add("c@c.com");
14
* msg.CC.Add("c@c.com");可以抄送给多人
15
*/
16
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8);
17
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
18
msg.Subject = "这是测试邮件";//邮件标题
19
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
20
msg.Body = "邮件内容";//邮件内容
21
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
22
msg.IsBodyHtml = false;//是否是HTML邮件
23
msg.Priority = MailPriority.High;//邮件优先级
24
25
SmtpClient client = new SmtpClient();
26
client.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
27
//上述写你的GMail邮箱和密码
28
client.Port = 587;//Gmail使用的端口
29
client.Host = "smtp.gmail.com";
30
client.EnableSsl = true;//经过ssl加密
31
object userState = msg;
32
try
33
{
34
client.SendAsync(msg, userState);
35
//简单一点儿可以client.Send(msg);
36
MessageBox.Show("发送成功");
37
}
38
catch (System.Net.Mail.SmtpException ex)
39
{
40
MessageBox.Show(ex.Message, "发送邮件出错");
41
}
42
}
43![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)
2
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](http://www.51aspx.com/Images/OutliningIndicators/InBlock.gif)
41
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](http://www.51aspx.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
43
![](http://www.51aspx.com/Images/OutliningIndicators/None.gif)