1. 程式人生 > >System.net.mail 騰訊ssl發送郵件超時

System.net.mail 騰訊ssl發送郵件超時

關於 lds write mtp throw subject update 賬號登錄 pda

我采用了.net 的自帶組件 System.Web.Mail.MailMessage發送郵件,主要是在客戶註冊網站成功的時候發條歡迎郵件,最近郵件無法發送了,看了下騰訊smtp郵件配置,所有的郵件發送都換成ssl了,之前用的是25端口,現在換成了465或587,於是修改代碼如下:

MailMessage msgMail = new MailMessage("發件箱", "收件箱", "郵件標題", "郵件內容",2);
SmtpClient smtp = new SmtpClient("smtp.qq.com", 465,2);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("發件箱", "發件箱登錄密碼",2);
try
{
smtp.Send(msgMail,2);
}
catch (Exception ex)
{
Console.WriteLine("發送完畢......",2);
}

這樣還是不行,報操作已超時錯誤 在國外的技術網站上看到一句話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發送郵件。

詳見我下面的2篇文章:

C#利用CDO.Message發送郵件

C#利用System.web.mail發送郵件

C#利用System.web.mail發送郵件

System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(,2);
try
{
mail.To = "收件人郵箱";
mail.From = "發件人郵箱";
mail.Subject = "subject";
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Body = "<font color=‘red‘>body</font>";

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1",2); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "發件人郵箱",2); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "發件人郵箱密碼",2); //set your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465,2);//set port
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true",2);//set is ssl
System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";
System.Web.Mail.SmtpMail.Send(mail,2);
//return true;
}
catch (Exception ex)
{
ex.ToString(,2);
}



C#利用CDO.Message發送郵件

如何引用CDO.Message? cod.message的引用位置: C:\Windows\System32\cdosys.dll

CDO.Message objMail = new CDO.Message(,2);
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"].Value = 465;//設置端口
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.qq.com";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "發送郵件賬號";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = "發送郵件賬號";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "發送郵件賬號";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "發送郵件賬號";
objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "發送郵件賬號登錄密碼";
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
objMail.Configuration.Fields.Update(,2);
objMail.Send(,2);
}
catch (Exception ex) { throw ex; }
finally { }
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail,2);
objMail = null;

System.net.mail 騰訊ssl發送郵件超時