哈哈哈 利用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

[c-sharp] view plain copy print ?
  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. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值