哈哈哈 利用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. Attachment a = new Attachment(@"C:/Users/Administrator/Desktop/smtpclient.rar");
  19. message.Attachments.Add(a);
  20. //设置邮箱的地址或IP
  21. SmtpClient client = new SmtpClient(server);
  22. //设置邮箱端口,pop3端口:110, smtp端口是:25
  23. client.Port = 25;
  24. //设置超时时间
  25. client.Timeout = 9999;
  26. //要输入邮箱用户名与密码
  27. client.Credentials = new NetworkCredential("young-20@163.com", "******");
  28. client.Send(message);
  29. }
  30. catch (Exception ex)
  31. {
  32. Success = ex.ToString();
  33. }
  34. return Success;
  35. }

2 QQ邮箱

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

HOST:smtp.qq.com

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

posted @ 2012-11-14 15:49  MFT  阅读(13359)  评论(1编辑  收藏  举报