System.net.mail使用ssl发送邮件失败
原创我采用了.net 的内置组件System.Net.Mail发送电子邮件,主要是在客户成功注册网站时,是发送欢迎电子邮件。最近邮件一直无法发送,请查看腾讯smtp电子邮件配置,所有发送的电子邮件都替换为ssl我以前用过它25端口,现在替换为465或587因此,请按如下方式修改代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
MailMessage msgMail = new MailMessage( "发件箱" , "收件箱" , "邮件标题" , "邮件内容" );
SmtpClient smtp = new SmtpClient( "smtp.qq.com" , 465);
smtp.EnableSsl = true ;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential( "发件箱" , "发件箱登录密码" );
try
{
smtp.Send(msgMail);
}
catch (Exception ex)
{
Console.WriteLine( "发送完毕......" );
}
这仍然不起作用,操作已超时错误 在国外科技网站上看到一句话System.Net.Mail支持Explicit SSL但它不受支持Implicit SSL然后,在检查了这两种模式的信息后,我将根据我的理解进行解释:
Explicit SSL 源自未加密25然后继续starttl握手并最终切换到加密连接。
Implicit SSL 直接从指定端口启动starttl握手。
既然指定了端口,那么应该就是使用了Implicit SSL我不知道微软什么时候可以更新它System.net.mail,System.net.mail可以在电子邮件中嵌入图像。此时,是不是无法使用腾讯邮箱发送电子邮件?答案肯定是否定的,foxmail是否可以配置发送电子邮件?我们可以利用CDO.Message和System.web.mail发送电子邮件。
C#利用CDO.Message发送邮件
如何引用CDO.Message? cod.message参考位置: C:\Windows\System32\cdosys.dll
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
CDO.Message objMail = new CDO.Message();
try
{
objMail.To = "接收电子邮件帐户" ;
objMail.From = "发送电子邮件帐户" ;
objMail.Subject = "subject" ; //邮件主题string strHTML = @"";
strHTML = strHTML + "你可以在这里填写html内容" ;
objMail.HTMLBody = strHTML; //邮件内容
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/smtpserverport](http://schemas.microsoft.com/cdo/configuration/smtpserverport) " ].Value = 465;//设置端口
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/smtpserver](http://schemas.microsoft.com/cdo/configuration/smtpserver) " ].Value = "smtp.qq.com" ;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/sendemailaddress](http://schemas.microsoft.com/cdo/configuration/sendemailaddress) " ].Value = "发送电子邮件帐户" ;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress](http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress) " ].Value = "发送电子邮件帐户" ;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/smtpaccountname](http://schemas.microsoft.com/cdo/configuration/smtpaccountname) " ].Value = "发送电子邮件帐户" ;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/sendusername](http://schemas.microsoft.com/cdo/configuration/sendusername) " ].Value = "发送电子邮件帐户" ;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/sendpassword](http://schemas.microsoft.com/cdo/configuration/sendpassword) " ].Value = "发送电子邮件帐户登录密码" ;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/sendusing](http://schemas.microsoft.com/cdo/configuration/sendusing) " ].Value = 2;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/smtpauthenticate](http://schemas.microsoft.com/cdo/configuration/smtpauthenticate) " ].Value = 1;
objMail.Configuration.Fields[ " [http://schemas.microsoft.com/cdo/configuration/smtpusessl](http://schemas.microsoft.com/cdo/configuration/smtpusessl) " ].Value = "true" ;//这一句指示是否使用ssl
objMail.Configuration.Fields.Update();
objMail.Send();
}
catch (Exception ex) { throw ex; }
finally { }
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
objMail = null ;
C#利用System.web.mail发送邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
try
{
mail.To = "收件人电子邮件" ;
mail.From = "发件人电子邮件" ;
mail.Subject = "subject" ;
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Body = "body" ;
mail.Fields.Add( " [http://schemas.microsoft.com/cdo/configuration/smtpauthenticate](http://schemas.microsoft.com/cdo/configuration/smtpauthenticate) " , "1" ); //basic authentication
mail.Fields.Add( " [http://schemas.microsoft.com/cdo/configuration/sendusername](http://schemas.microsoft.com/cdo/configuration/sendusername) " , "发件人电子邮件" ); // set your username here
mail.Fields.Add( " [http://schemas.microsoft.com/cdo/configuration/sendpassword](http://schemas.microsoft.com/cdo/configuration/sendpassword) " , "发件人电子邮件密码" ); // set your password here
mail.Fields.Add( " [http://schemas.microsoft.com/cdo/configuration/smtpserverport](http://schemas.microsoft.com/cdo/configuration/smtpserverport) " , 465);// set port
mail.Fields.Add( " [http://schemas.microsoft.com/cdo/configuration/smtpusessl](http://schemas.microsoft.com/cdo/configuration/smtpusessl) " , "true" );// set is ssl
System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com" ;
System.Web.Mail.SmtpMail.Send(mail);
//return true;
}
catch (Exception ex)
{
ex.ToString();
}
转自: https://www.ie81.com/Technology/225.html
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
itfan123



