C# SmtpClient 发邮件
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 发邮件的更多相关文章
- SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender
今天在看C#高级编程(第9版)的时候,在768页看到这样的一段代码 SmtpClient sc = new SmtpClient(); sc.Host = "邮箱服务器地址"; M ...
- C# .net 使用 SmtpClient 发邮件 ,发送邮箱的配置
1.需打开POP3/SMTP/IMAP 2.打开时要求授权码,输入自定义的密码如:1234cb 3.自定义的密码就是 SmtpClient 的密码,而非邮箱密码
- .net发邮件【转】
对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help. // 引入命名空间using Syste ...
- C#后台程序重启IIS,发邮件通知
应用场景:IIS网站挂掉,系统自动重启IIS,通知相关联系人: 主要代码: 监控类 public class monitoringiis { EmailSend send = new EmailSen ...
- C# 发邮件类可发送附件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...
- .Net 发邮件
对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help. // 引入命名空间using Syste ...
- c# 发邮件功能
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using Sy ...
- 杂项收集,包括-发邮件、二维码生成、文件下载、压缩、导出excel
本篇就最近工作解决的问题做个代码收集.包括以下几个方面:发邮件.二维码生成.文件下载.压缩.导出excel.有一种可用的解决方法就好,不求全面,不求原理. 1.发邮件: 命名空间:System.Net ...
- C# System.Net.Mail.MailMessage 发邮件
C# System.Net.Mail.MailMessage 发邮件 上篇文化在哪个可以看到使用 System.Web.Mail.MailMessage 发邮件时会提示 ,提供用于构造电子邮件的属性和 ...
随机推荐
- C# consume RestApi
1.RestSharp. Nuget install RestSharp,Newtonsoft.Json. using System; using RestSharp; using Newtonsof ...
- oracle 利用序列与触发器实现列自增
实现步骤:先创建序列,后创建触发器 1.创建序列 create sequence 序列名 increment start maxvalue ; 2.创建触发器 create or replace tr ...
- css3自适应布局单位vw,vh详解
视口单位(Viewport units) 什么是视口? 在桌面端,视口指的是在桌面端,指的是浏览器的可视区域:而在移动端,它涉及3个视口:Layout Viewport(布局视口),Visual Vi ...
- SAP HUMO VLMOVE不支持E库存发货到成本中心
SAP HUMO VLMOVE不支持E库存发货到成本中心 如下HU 194810300235,里面物料的库存是E库存, 执行VLMOVE,试图将该HU整托发货到成本中心, 输入成本中心后,点击按钮'P ...
- [b0001] 伪分布式 hadoop 2.6.4
说明: 任务:搭建Hadoop伪分布式版本. 目的:快速搭建一个学习环境,跳过这一环境,快速进入状态,使用Hadoop一些组件做些任务 没有选择2.7,觉得bug比较多,不稳定. 选择伪分布式简单快速 ...
- Linux 查找目录下大于*M的文件
1. 查找指定文件夹下等于1M的文件 find ./ -size 1M | wc -l 2. 查找指定文件夹下大于1M的文件 find ./target_path -size +1M | wc -l
- C学习笔记(10)--- 强制类型转换,错误处理,递归
1.强制类型转换: 强制类型转换是把变量从一种类型转换为另一种数据类型.例如,如果您想存储一个 long 类型的值到一个简单的整型中,您需要把 long 类型强制转换为 int 类型. 您可以使用强制 ...
- firefox56 版本中的 Selenium IDE 无法导出脚本问题
firefox:56 Slenium IDE :3系列 问题:Selenium IDE 没有工具栏,无法导出录制的脚本,这给自动化测试工作带来了极大的不便. 解决办法:将firefox 降级 (只有5 ...
- react界面跳转,滚动到顶部
在使用react-router-dom时,我们经常会遇到路由切换时滚动到浏览器顶部的问题. 滚动到顶部 Scroll to top 很多时候我们需要的是滚动到顶部“Scroll to top”,因为发 ...
- HTML与CSS学习笔记(4)
1.浏览器前缀 浏览器厂商以前就一直在实时CSS3,但它还未成为真正的标准.为此,当有一些CSS3样式与法还在波动的时候,他们提出了针对不同浏览器的前缀,来解决兼容性问题. 即:CSS3去兼容不同的浏 ...