通过.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://51aspx.com/Images/OutliningIndicators/None.gif)
![](http://51aspx.com/Images/OutliningIndicators/None.gif)
2
![](http://51aspx.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](http://51aspx.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
39
![](http://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
}
![](http://51aspx.com/Images/OutliningIndicators/None.gif)
2
![](http://51aspx.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
34
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](http://51aspx.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
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://51aspx.com/Images/OutliningIndicators/None.gif)
![](http://51aspx.com/Images/OutliningIndicators/None.gif)
2
![](http://51aspx.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
4
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](http://51aspx.com/Images/OutliningIndicators/InBlock.gif)
41
![](http://51aspx.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](http://51aspx.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
43
![](http://51aspx.com/Images/OutliningIndicators/None.gif)