using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
using System.Threading;
using System.IO;
using System.Security.Cryptography; namespace ConsoleApp348
{
class Program
{
static string logFullPath = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
static void Main(string[] args)
{
Timer emailTimer = new Timer(SendMailCallBack, null, , );
Console.ReadLine();
} private static void SendMailCallBack(object state)
{
string fromMail = "fromMail";
string fromMailPwd = "fromMailPwd";
string toMail = "toMail";
MailExample(fromMail, fromMailPwd, toMail);
} static void MailExample(string fromEmail, string fromEmailPwd, string toEmail)
{
try
{
using (SmtpClient smtpClient = new SmtpClient())
{
smtpClient.Host = "smtp.163.com";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(fromEmail, fromEmailPwd);
using (MailMessage mail = new MailMessage())
{
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.From = new MailAddress(fromEmail);
mail.To.Add(new MailAddress(toEmail));
mail.Subject = $"This is the new test.Now is {DateTime.Now.ToString("mmssffff")}";
mail.Body = $"This is the mail body,And now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}";
smtpClient.SendAsync(mail, $"Frommail:{fromEmail},ToMail:{toEmail},send email successfully!");
smtpClient.SendCompleted += SmtpClient_SendCompleted;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
LogMessage(ex.StackTrace);
}
} private static void SmtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Console.WriteLine(e.UserState);
LogMessage(e.UserState.ToString());
} static void LogMessage(string msg)
{
lock(logFullPath)
{
using (StreamWriter logWriterStream = new StreamWriter(logFullPath, true, UTF8Encoding.UTF8))
{
logWriterStream.WriteLine($"Log message :{msg},now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
}
}
}
}
}

异步: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. go-控制语句

    if else else不能换行 if后最好不加小括号,当然可以加,但建议不加 求平方根 引入math包 调用math.Sqrt(num)函数 switch分支 不用加break来跳出,每一个case ...

  2. 易优CMS:volist的基础应用

      [基础用法] 名称:volist 功能:数据/记录循环输出标签 语法: {eyou:channel type='top'}            {eyou:volist name='$field ...

  3. Mybatis中动态SQL语句中的parameterType不同数据类型的用法

    Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型,    此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...

  4. c++ lesson 一(命名空间输入输出)

    // // main.cpp // C++lessonOne // // Created by keyan on 15/11/13. // Copyright © 2015年 keyan. All r ...

  5. SQL学习_SQL函数

    常用的 SQL 函数 1. 算术函数 SELECT ABS(-2),运行结果为 2 SELECT MOD(101,3),运行结果 2 SELECT ROUND(37.25,1),运行结果 37.3 2 ...

  6. [b0033] python 归纳 (十八)_队列Queue在多线程中使用(二)

    # -*- coding: UTF-8 -*- """ 多线程同时读队列 使用 join(), task_done() 逻辑: 3个子线程并发 从有6个数据的队列中取数据 ...

  7. python 将带有TZ的UTC时间字符串,转换成本地时间

    整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用协调时(UTC, Universal Time Coordinated).UTC与格林尼治 ...

  8. Mybatis逆向工程中的 mybatis-generator:generate 代码生成器的使用

    使用逆向工程可以根据数据库的表名字生成pojo层(实体类),mapper层(dao层,直接与底层的XML中映射相关),XML(映射执SQL语句) 下面请看具体生成步骤 1. 点击generatorCo ...

  9. .gitignore 文件没起作用

    场景 修改了.gitignore文件, 但是查看状态还是没有忽略 解决 *. 清除git缓存, 注意最后有一个点 git rm -r --cache . 再之后就可以正常使用了, 基本恢复正常, 被忽 ...

  10. 4. Vue - 指令(Add)

    一.指令系统 1. v-text ​ v-text主要用来更新textContent,可以等同于JS的text属性. <span v-text="msg"></s ...