C# 发送邮件的记录(qq,126,Gmail)
需求:用C#做一个发送邮件的接口。
说明:
1.smtp
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。通过SMTP协议所指定的服务器,就可以把E-mail寄到收信人的服务器上了,整个过程只要几分钟。SMTP服务器则是遵循SMTP协议的发送邮件服务器,用来发送或中转发出的电子邮件。
这是原理说明(蒙蔽,有时间再研究下)
http://blog.csdn.net/kerry0071/article/details/28604267
2.smtp服务器地址和端口
25:如果不启用SSL认证,一般是这个端口;
587/465:启动SSL认证。
以下是常用邮箱的端口:
http://blog.csdn.net/whyhonest/article/details/7289420
QQ:smtp.qq.com:465
126:smtp.126.com:25
Gmail:smtp.gmail.com:587
步骤:
1.先启动邮箱smtp功能:
QQ(126类似)–这两个要启用SSL都需要专门的授权码,可以在邮箱通过账号获取。
http://jingyan.baidu.com/article/0f5fb099dffe7c6d8334ea31.html
Gmail:
需要fan墙,操作类似;除了在邮箱页面进行设置之外,还需要在客户端进行设置。(win10 用OutLook比较方便)
参考:http://www.xujiahua.com/848.html
官方:https://support.google.com/mail/answer/7126229?visit_id=1-636278354684802140-826274024&hl=zh-Hans&rd=1
2.编码部分:在C#中QQ 和 126/Gmail 所用类库不一样。QQ用System.Web.Mail;126用System.Net.Mail(QQ用会出操作超时的异常)。
QQ
public static void SendQQEmailtest(string server, int port, string sender, string recipient, string subject, string body, bool isBodyHtml, Encoding encoding, string authentication, string userName, params string[] files)
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.To = recipient;
mail.From = sender;
mail.Subject = subject;
mail.BodyFormat = System.Web.Mail.MailFormat.Text;
mail.Body = body;
//添加附件
mail.Attachments.Clear();
if (files != null && files.Length != 0)
{
for (int i = 0; i < files.Length; ++i)
{
Attachment attach = new Attachment(files[i]);
mail.Attachments.Add(attach);
}
}
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", authentication); //这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //邮箱登录账号,这里跟前面的发送账号一样就行
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);//端口
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密
System.Web.Mail.SmtpMail.SmtpServer = server; //企业账号用smtp.exmail.qq.com
System.Web.Mail.SmtpMail.Send(mail);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Gmail/126
public static void Send2test(string server, int port, string sender, string recipient, string subject,
string body, bool isBodyHtml, Encoding encoding, string authentication, string userName, params string[] files)
{
//确定发件人地址、收件人地址
MailMessage message = new MailMessage(sender, recipient);
message.IsBodyHtml = isBodyHtml;
message.SubjectEncoding = encoding;
message.BodyEncoding = encoding;
message.Subject = subject;
message.Body = body;
message.Attachments.Clear();
if (files != null && files.Length != 0)
{
for (int i = 0; i < files.Length; ++i)
{
Attachment attach = new Attachment(files[i]);
message.Attachments.Add(attach);
}
}
//根据 邮件服务器 地址建立客户端
SmtpClient smtpClient = new SmtpClient(server, port);//如果不写端口就会出现异常:AUTH fist!
smtpClient.Timeout = 50000;
smtpClient.EnableSsl = true;//从抓包的情况来看是有STARTTLS的
if (string.IsNullOrEmpty(authentication))
{
smtpClient.UseDefaultCredentials = true;
}
else
{
//smtpClient.UseDefaultCredentials = true;//看来这句是可有可无的
smtpClient.Credentials = new NetworkCredential(userName,
authentication);
}
//smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//通过网络发送
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
}
finally{
smtpClient.Dispose();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
有意思的是在Gmail中:smtpClient.UseDefaultCredentials = true 在官方的解释中:
如果 UseDefaultCredentials 属性设置为 false,则连接到服务器时会将 Credentials 属性中设置的值用作凭据。 如果 UseDefaultCredentials 属性设置为 false并且尚未设置 Credentials 属性,则将邮件以匿名方式发送到服务器。
然而调试中会出异常:
其他信息: SMTP 服务器要求安全连接或客户端未通过身份验证。 服务器响应为:5.5.1 Authentication Required.
当注释掉smtpClient.UseDefaultCredentials这句后,就能发送邮件了。
PS:关于其中遇到的问题及解决:
1.126
不允许使用邮箱名称。 服务器响应为:authentication is required,126 smtp4,
原因:
输入了地址,但是port没有明确|没有输入验证信息
-所以输入对应的port + 确认验证信息
2.关于Gmail这个大坑
1.调试中是否需要翻墙:
否;否则在连接服务器的时候会出现各种失败;
2.Gmail端口
587而非465!否则会报操作超时;
3.Gmail-tls
在C#中,smtpClient.EnableSsl = true;相当于启用了tls,从wireshark中可以看出STARTTLS。不用花心思去琢磨用C#实现tls。以下是原理:
4.是否需要修改Hosts
参考:http://www.williamlong.info/archives/4455.html
发现其中关于修改是针对OutLook的。我在实际操作中是修改了Hosts之后操作的OutLook,并且在该环境中进行调试并发送邮件成功的。但是会存在不稳定的问题,有时候会显示操作超时,但是邮件仍然收不到的情况;
待还原Hosts文件后再测试下效果。
5.Gmail有可能要设置“允许不够安全的应用访问”
参考文章:
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000
于是我坚定了从587端口尝试的决心 %>_<%,不过看起来Python好方便啊。465端口在尝试中发现与服务器连接各种失败、操作超时。
http://stackoverflow.com/questions/2057227/c-sharp-asp-net-send-email-via-tls
于是我放弃了琢磨TLS
http://qa.helplib.com/198009
于是我开始怀疑官方文档。。并且这里的答案是可信的。而且经过尝试,smtpClient.UseDefaultCredentials 可以不用管它。
2017年4月24日 更新
后来为了解决System.web.mail过时方法的警告,采用了另外的解决方案。
http://www.codingwhy.com/view/614.html
public static void Send(string server, int port, string sender, string password, string recipient, string subject, string body, bool isBodyHtml, params string[] files)
{
CDO.Message objMail = new CDO.Message();
try
{
objMail.To = recipient;
objMail.From = sender;
objMail.Subject = subject;//邮件主题string strHTML = @"";
if (isBodyHtml == true)
{
//strHTML = strHTML + "这里可以填写html内容";
objMail.HTMLBody = body;//邮件内容
}
else
{
objMail.TextBody = body;
}
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = port;//设置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = server;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = sender;//"发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = sender;//"发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = sender;//"发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = sender;//"发送邮件账号";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;//"发送邮件账号登录密码";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//这一句指示是否使用ssl
if (files != null && files.Length != 0)
{
for (int i = 0; i < files.Length; ++i)
{
objMail.AddAttachment(files[i], "", "");//"C:\\Hello.txt"
}
}
objMail.Configuration.Fields.Update();
objMail.Send();
}
catch (Exception ex) { throw ex; }
finally { objMail = null; }
}
原文:https://blog.csdn.net/u013244192/article/details/70194595
C# 发送邮件的记录(qq,126,Gmail)相关推荐
- php 邮件发送设置_PHP实现自动发送邮件功能代码(qq 邮箱)
最近做一个邮箱验证的功能,研究了一会,搞定了邮件的自动发送.下面用qq邮箱作为演示,一步一步来解释: 代码下载地址 首先,就是做到邮件的发送,代码如下: //邮件发送 require "./ ...
- 邮箱注册(发送邮件验证码;QQ邮箱)
邮箱注册(发送邮件验证码:QQ邮箱) 先去QQ邮箱-->点击设置有POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务.开通会有授权码 复制粘贴代码就可以使用.根据指 ...
- python发送邮件 python发送qq,163,sohu, xinlang, 126等邮件 python自动发邮件总结及实例说明...
python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email模块主要负责构造邮件. sm ...
- 线上发送邮件问题记录(Could not connect to SMTP host:smtp.exmail.qq.com,port:465)
事件回顾到某天下班,然后听到手机消息,来自企业微信,是财务小姐姐向我发来了问候,问我为什么邮件突然就发不出去了. 我也愣住了,这个功能是去年做的了,一直没有什么问题,甚至另一个系统也是copy我的代码 ...
- 常识-java发送邮件函数+开启qq邮箱授权码
文章目录 前言 开启邮箱授权码 使用邮箱和授权码创建发邮件的客户端 前言 一个可以向指定邮箱地址发送邮件的函数 假设有a,b两个邮箱 a邮箱可以作为发送者,b邮箱随意,a邮箱只需要获得授权码,就可以向 ...
- Shell脚本发送邮件(CentOS+mailx+QQ邮箱)
1. mailx 1. 准备工作 打开邮箱设置,开启pop3/smtp服务和imap/smtp服务 安装mailx:yum install -y mailx 2. 配置 设置/etc/mail.rc文 ...
- 怎样查询计算机登录记录,qq登陆记录,教您QQ如何查看登录历史记录
qq是我们经常会使用到的一款聊天工具,很多用户都会使用到它.不过,最近一些朋友反馈自己想要在电脑中查看qq登录记录,可是操作了很久都没有成功.当我们的QQ出现异常登陆的时候我们往往会想要查询一下登陆记 ...
- java gmail 发送邮件_使用JavaMail对Gmail进行邮件收发
进行JavaMail 收发邮件,必须导入2个Jar包 Mail.Jar Activation.Jar //利用JavaMail收/发Gmail邮件(SSL) //Gmail目前已经启用了POP3和SM ...
- 尝试用程序记录QQ密码
今天就QQ的密码记录程序找了下资料,发现网络上的对QQ密码的记录程序都已失效,原因是腾讯目前的所谓的"国际领先的Nprotect键盘加密技术"... 不过牛人也通过Hook API ...
最新文章
- SQL SERVER 优化 50法
- LiveVideoStack冬季招聘(高级策划编辑,市场BD主管)
- TCL:花开刹那还是浴火重生
- asp.net2.0跨域问题
- 工作41:解决vuex刷新数据丢失
- pthreads v3下一些坑和需要注意的地方
- 获取SD卡上 未安装 APK文件信息
- 突然出现 -bash: pod: command not found 的解决方法
- 将React Native集成至Android原生应用
- 百度离线地图服务器搭建
- mac设置windows文件服务器,苹果MAC访问Windows共享文件夹的技巧
- 支付宝手机网站支付详细流程
- 论文笔记:Exploiting Cloze Questions for Few Shot Text Classification and Natural Language Inference
- Python3之数据结构
- 塔望食业洞察|人参饮料行业环境 市场现状及发展思考
- 褚橙是如何用互联网营销颠覆橙子的?
- Airsim 无人机仿真
- QCustomPlot 示例实践--带填充的简单衰减正弦函数及其红色的指数包络
- 有道云笔记 - Markdown模板(文首附markdown源码,即.md文件)
- 全球与中国小龙虾市场深度研究分析报告
热门文章
- luci L大_“大众”果然没失望,空间大,颜值暴增
- UE4学习-第三人称游戏的AI巡逻
- MySQL更新会影响查询吗_mysql更新查询不会执行
- 系统无法在消息文件中为application_iOS 14 Filza 文件消息,M1 能用 win 系统
- android搭建https,android 搭建https Server(示例代码)
- 全国大学生数学建模2014年A题嫦娥三号软着陆轨道设计与控制策略论文与代码
- sqlserver 集群_云数据库最优成本方案,阿里云数据库新形态专属集群
- idea中生成spring的 xml配置文件_【132期】面试再被问到Spring容器IOC初始化过程,就拿这篇文章砸他~...
- vivado不识别HLS生成的IP解决方法
- linux mysql5.1 安装_linux编译安装mysql5.1.x