利用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.             }  

 

posted @ 2012-10-26 16:13  BellingWP  阅读(217)  评论(0编辑  收藏  举报