利用SmtpClient发送邮件

1 163邮箱 HOST:smtp.163.com

1.public static string CreateTimeoutTestMessage(string server) 
2.{ 
3.string Success = "发送成功"; 
4.try 
5.{ 
6.string _to = "1035092449@qq.com"; 
7.string _from = "young-20@163.com"; 
8.string _subject = "Using the new SMTP client."; 
9.string _body = @"Using this new feature, you can send an e-mail message from an application very easily.";
 10.MailMessage message = new MailMessage(); 
11.message.From = new MailAddress(_from); 
12.//可以利用MailMessage.To.Add方法增加要发送的邮件地址 
13.message .To .Add (new MailAddress ("652105072@qq.com")); 
14.message.To.Add(new MailAddress(_to)); 
15.message.Subject = _subject; 
16.message.Body = _body; 
17.
18.//添加附件 
19.Attachment a = new Attachment(@"C:/Users/Administrator/Desktop/smtpclient.rar");
 20.message.Attachments.Add(a); 
21.//设置邮箱的地址或IP 
22.SmtpClient client = new SmtpClient(server); 
23.//设置邮箱端口,pop3端口:110, smtp端口是:25 
24.client.Port = 25; 
25.
26.//设置超时时间 
27.client.Timeout = 9999; 
28.
29.//要输入邮箱用户名与密码 
30.
31.client.Credentials = new NetworkCredential("young-20@163.com", "******"); 
32.client.Send(message); 
33.} 
34.catch (Exception ex) 
35.{ 
36.Success = ex.ToString(); 
37.} 
38.return Success; 
39.} 

2 QQ邮箱

QQ邮箱默认的SMTP服务是关闭的,要自己去开通。

HOST:smtp.qq.com

1.try 
2.{ 
3.
4.SmtpClient client = new SmtpClient(); 
5.client.Host = "smtp.qq.com"; 
6.MailMessage mm = new MailMessage(); 
7.client.Port = 25; 
8.
9.mm.From = new MailAddress("652105072@qq.com"); 
10.mm.To.Add(new MailAddress("1035092449@qq.com")); 
11.mm.Subject = "Hello~!"; 
12.mm.Body = "HIthere.here is a post "; 
13.mm.IsBodyHtml = false; 
14.mm.Priority = MailPriority.High; 
15.
16.client.Credentials = new NetworkCredential("652105072@qq.com", "******"); 
17.client .Send (mm); 
18.
19.
20.} 
21.catch (Exception ex) 
22.{ 
23.MessageBox.Show(ex.Message); 
24.} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值