qq.smtp.com发送超时

使用qq的smtp左右发送邮箱,发现.NET自带的SmtpClient无法支持带ssh的,原因分析如下:

 

报操作已超时错误 在国外的技术网站上看到一句话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发送邮件。

 

 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net.Mail;
 5 using System.Text;
 6 using Tunynet.Email;
 7 
 8 namespace Spacebuilder.Common
 9 {
10     public static class CDEmailServiceExtensions
11     {
12       
13 //报操作已超时错误 在国外的技术网站上看到一句话System.Net.Mail支持Explicit SSL但是不支持Implicit SSL,然后查了下关于这两个模式的资料,我按照我理解的说一下: 
14 
15 //Explicit SSL 发起于未加密的25,然后进行一个starttl握手,最终切换到加密的连接。
16 
17 //Implicit SSL 直接从指定的端口发起starttl握手。 
18 
19 //既然指定了端口,那么应该就是使用了Implicit SSL,不知道微软什么时候能更新下System.net.mail,System.net.mail能在邮件中嵌入图片的。问题到了这里,那是不是就没有办法利用腾讯邮箱发邮件了呢?答案肯定是否定的,foxmail不就可以配置发送邮件吗?我们可以利用CDO.Message和System.web.mail发送邮件。
20         /// <summary>
21         /// t通过CDO方式发生,支持Implicit SSL
22         /// </summary>
23         /// <param name="service"></param>
24         /// <param name="id"></param>
25         /// <param name="Subject"></param>
26         /// <param name="sendContent"></param>
27         /// <param name="errorMessage"></param>
28         public static bool CDO_StmpSettings(this EmailService service, SmtpSettings smtpSettings, MailMessage mailMsg,out string errorMessage)
29         {
30             string Subject=mailMsg.Subject;
31             string sendContent = mailMsg.Body;
32 
33 
34             string sender = mailMsg.From.Address; //mailMsg.Sender.Address;
35 
36             bool isSendSuccess = false;
37 
38             errorMessage = string.Empty;
39            // SmtpSettings smtpSettings = service.GetSmtpSettings(id);
40             CDO.Message objMail = new CDO.Message();
41             
42             try
43             {
44                 objMail.To = mailMsg.To.First().Address;//"接收邮件账号";
45                 objMail.From = sender;//"发送邮件账号";
46                 objMail.Subject = Subject;//邮件主题string strHTML = @"";
47                 objMail.HTMLBody = sendContent;//邮件内容
48                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = smtpSettings.Port;//465;//设置端口
49                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value =smtpSettings.Host;// "smtp.qq.com";
50                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value =smtpSettings.UserEmailAddress;// "发送邮件账号";
51                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = smtpSettings.UserEmailAddress;//"发送邮件账号";
52                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value =smtpSettings.UserEmailAddress;// "发送邮件账号";
53                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value =smtpSettings.UserEmailAddress;// "发送邮件账号";
54                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value =smtpSettings.Password;// "发送邮件账号登录密码";
55                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
56                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
57                 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = smtpSettings.EnableSsl.ToString();//这一句指示是否使用ssl
58                 objMail.Configuration.Fields.Update();
59                 objMail.Send();
60                 isSendSuccess = true;
61             }
62             catch (Exception ex) { 
63                 errorMessage=ex.Message;
64             }
65             finally {
66                 System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
67                 objMail = null;
68             }
69             return isSendSuccess;
70            
71         }
72     }
73 }

 



posted @ 2016-12-20 16:38  gaofeidemeng  阅读(1027)  评论(0编辑  收藏  举报