1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Mail;
  7. using System.Net;
  8. using System.Threading;
  9. using System.IO;
  10. using System.Security.Cryptography;
  11.  
  12. namespace ConsoleApp348
  13. {
  14. class Program
  15. {
  16. static string logFullPath = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
  17. static void Main(string[] args)
  18. {
  19. Timer emailTimer = new Timer(SendMailCallBack, null, , );
  20. Console.ReadLine();
  21. }
  22.  
  23. private static void SendMailCallBack(object state)
  24. {
  25. string fromMail = "fromMail";
  26. string fromMailPwd = "fromMailPwd";
  27. string toMail = "toMail";
  28. MailExample(fromMail, fromMailPwd, toMail);
  29. }
  30.  
  31. static void MailExample(string fromEmail, string fromEmailPwd, string toEmail)
  32. {
  33. try
  34. {
  35. using (SmtpClient smtpClient = new SmtpClient())
  36. {
  37. smtpClient.Host = "smtp.163.com";
  38. smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
  39. smtpClient.UseDefaultCredentials = false;
  40. smtpClient.Credentials = new System.Net.NetworkCredential(fromEmail, fromEmailPwd);
  41. using (MailMessage mail = new MailMessage())
  42. {
  43. mail.BodyEncoding = UTF8Encoding.UTF8;
  44. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  45. mail.From = new MailAddress(fromEmail);
  46. mail.To.Add(new MailAddress(toEmail));
  47. mail.Subject = $"This is the new test.Now is {DateTime.Now.ToString("mmssffff")}";
  48. mail.Body = $"This is the mail body,And now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}";
  49. smtpClient.SendAsync(mail, $"Frommail:{fromEmail},ToMail:{toEmail},send email successfully!");
  50. smtpClient.SendCompleted += SmtpClient_SendCompleted;
  51. }
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. Console.WriteLine(ex.StackTrace);
  57. LogMessage(ex.StackTrace);
  58. }
  59. }
  60.  
  61. private static void SmtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  62. {
  63. Console.WriteLine(e.UserState);
  64. LogMessage(e.UserState.ToString());
  65. }
  66.  
  67. static void LogMessage(string msg)
  68. {
  69. lock(logFullPath)
  70. {
  71. using (StreamWriter logWriterStream = new StreamWriter(logFullPath, true, UTF8Encoding.UTF8))
  72. {
  73. logWriterStream.WriteLine($"Log message :{msg},now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
  74. }
  75. }
  76. }
  77. }
  78. }

异步:public void SendAsync(MailMessage message, object userToken);

Sends the specified e-mail message to an SMTP server for delivery. This method  does not block the calling thread and allows the caller to pass an object to  the method that is invoked when the operation completes.

C# SmtpClient 发邮件的更多相关文章

  1. SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender

    今天在看C#高级编程(第9版)的时候,在768页看到这样的一段代码 SmtpClient sc = new SmtpClient(); sc.Host = "邮箱服务器地址"; M ...

  2. C# .net 使用 SmtpClient 发邮件 ,发送邮箱的配置

    1.需打开POP3/SMTP/IMAP 2.打开时要求授权码,输入自定义的密码如:1234cb 3.自定义的密码就是  SmtpClient 的密码,而非邮箱密码

  3. .net发邮件【转】

    对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help. // 引入命名空间using Syste ...

  4. C#后台程序重启IIS,发邮件通知

    应用场景:IIS网站挂掉,系统自动重启IIS,通知相关联系人: 主要代码: 监控类 public class monitoringiis { EmailSend send = new EmailSen ...

  5. C# 发邮件类可发送附件

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...

  6. .Net 发邮件

    对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help. // 引入命名空间using Syste ...

  7. c# 发邮件功能

    using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using Sy ...

  8. 杂项收集,包括-发邮件、二维码生成、文件下载、压缩、导出excel

    本篇就最近工作解决的问题做个代码收集.包括以下几个方面:发邮件.二维码生成.文件下载.压缩.导出excel.有一种可用的解决方法就好,不求全面,不求原理. 1.发邮件: 命名空间:System.Net ...

  9. C# System.Net.Mail.MailMessage 发邮件

    C# System.Net.Mail.MailMessage 发邮件 上篇文化在哪个可以看到使用 System.Web.Mail.MailMessage 发邮件时会提示 ,提供用于构造电子邮件的属性和 ...

随机推荐

  1. SPA项目开发之动态树、表格、分页

    思路: 1.准备好后台(左侧树,带分页的文章查询) 2.将左侧树的数据绑定到elementui中的menu标签上 3.新增一个自定义组件用来展示文章列表的 4.绑定elementui提供的分页组件来完 ...

  2. Java生鲜电商平台-SpringCloud微服务架构中网络请求性能优化与源码解析

    Java生鲜电商平台-SpringCloud微服务架构中网络请求性能优化与源码解析 说明:Java生鲜电商平台中,由于服务进行了拆分,很多的业务服务导致了请求的网络延迟与性能消耗,对应的这些问题,我们 ...

  3. DataGridView中实现点击单元格Cell动态添加自定义控件

    场景 鼠标点击DataGridView的某个单元格时,此单元格添加一个自定义的控件,这里以 添加下拉框为例 效果 注: 博客主页: https://blog.csdn.net/badao_liuman ...

  4. DQL---连接查询(内连接、外连接)、子查询、分页查询

    一.连接查询 1.连接查询建立在有相互关系的两个表间,进行两个及两个以上的表或视图的查询. 2.对n张表进行查询,至少需要n-1个连接表的条件. 二.笛卡尔积(容易造成数据库宕机) 1.指表中每行元素 ...

  5. Qt 显示图片

    QImage qImag("img.jpg"); //qImag = qImag.scaled(width, height); //缩放图片 qImag = qImag.scale ...

  6. /cygdrive/c/MinGW/bin/autoconf-2.68: line 501: /mingw/bin/autom4te-2.68: No such file or directory

    出现如下错误 编译openssh的时候 # autoconf /cygdrive/c/MinGW/bin/autoconf-2.68: line 501: /mingw/bin/autom4te-2. ...

  7. Android开发利器之pidcat

    介绍pidcat: pidcat 是Android届JakeWharton大神开发的一款命令行工具,堪称Android开发利器,它能方便Android程序猿捕获日志,过滤日志,定位程序问题,超级好用. ...

  8. Kotlin介绍(非原创)

    文章大纲 一.Kotlin简介二.Kotlin相比Java优势三.Kotlin与Java混合使用四.参考文章   一.Kotlin简介 1. 什么是Kotlin 安卓和Java,前者是最受欢迎的移动开 ...

  9. Android,百度,云知声tts总结

    最近在做Android语音播报功能(TTS),现总结如下:(ps:demo代码地址:https://github.com/giserlong/TTS_DEMO) 一.Android原生接口 用Andr ...

  10. CMD控制台如何把XXX.sql导入到数据库

    mysql -u root -proot <"F:\Git\cyb.sql" 注意:mysql -u 用户名 -p没有空格加上密码 <"sql文件的路径&qu ...