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. Python中全局变量的引用与修改之格式影响

    先来看下面的代码及执行结果: a = 1 b = [2,3] def nums(): a = 2 b[0] = 0 print(a) print(b) print(a) print(b) nums() ...

  2. 机器学习实战:基于Scikit-Learn和TensorFlow 读书笔记 第6章 决策树

    数据挖掘作业,要实现决策树,现记录学习过程 win10系统,Python 3.7.0 构建一个决策树,在鸢尾花数据集上训练一个DecisionTreeClassifier: from sklearn. ...

  3. 【LeetCode】1056-易混淆数

    易混淆数 给定一个数字 N,当它满足以下条件的时候返回 true:把原数字旋转180°以后得到新的数字.如 0, 1, 6, 8, 9 旋转 180° 以后,得到了新的数字 0, 1, 9, 8, 6 ...

  4. jenkins解决乱码

    1.Jenkins系统设置中修改 点击左侧“系统管理”——右侧选择“系统设置”——“全局属性”,选择第一项:Environment variables,键值对列表,点击增加: 键:LANG 值:zh. ...

  5. 重温《NoSQL精粹》

    前言 在网上有关Redis相关文章满天飞的时候,它是什么,用于解决什么问题,有哪些相类似的技术,与传统的关系型数据库有哪些差别,什么时候使用?这个时候我决定重温一下<NoSQL精粹>. 也 ...

  6. linux 创建虚拟块设备,制作文件系统并挂载,用于测试lustre

    1.制作块文件 3 个 [root@localhost yaoxu]# [root@localhost yaoxu]# [root@localhost yaoxu]# 2.创建回环设备 [root@l ...

  7. 20.Java基础_对象的构造函数

    package pack1; public class Student { private String name; private int age; //构造方法 //如果没有任何构造函数,系统会自 ...

  8. c# WF 第8节 label控件

    本节内容: 1: 文本控件 2:实现label的你追我赶实例 1:文本控件 2:实现label的你追我赶实例 步骤1 : 步骤2 : 知识点:

  9. 面向对象程序设计(Java) 第7周学习指导及要求

    2019面向对象程序设计(Java)第7周学习指导及要求 (2019.10.11-2019.10.14) 学习目标 掌握四种访问权限修饰符的使用特点: 掌握Object类的用途及常用API: 掌握Ar ...

  10. 攻防世界Web-bug

    一直误以为是二次注入,看了别人wp,自己梳理了一遍 首先打开题目页面 先注册一个账号 注册成功(注意这个UID) 然后注意下包,发现cookie中的user很可疑,是一串md5值,我们可以推测是我们注 ...