一、問題解惑,為什么465發送失敗
查閱資料得知,.net 的自帶組件System.Net.Mail發送郵件支持Explicit SSL但是不支持Implicit SSL,國內大部門郵件服務器都是Implicit SSL,所以無法通過465端口發郵件
有人說了,那干嘛要用呢,我用25不好好的么,為甚惡魔不用25呢?
這個問題問得好,很多雲服務器像阿里、騰訊購買的新機都是把25端口封禁的,想要用25端口需要手動申請解封,據阿里工作客服所說,審核通過率感人
那么今天就記錄一下如何使用465端口成功發郵件
二、解決方案
1、可以使用CDO.Message發送郵件
如何引用CDO.Message? cod.message的引用位置: C:\Windows\System32\cdosys.dll
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"].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();
objMail.Send();
}
catch (Exception ex) { throw ex; }
finally { }
System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
objMail = null;
2、使用System.web.mail發送郵件(僅適用於Web應用程序)
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", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "發件人郵箱"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "發件人郵箱密碼"); //set your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//set port
mail.Fields.Add("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();
}
3、使用MailKit
需要NuGet兩個包MimeKit、MailKit
using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.IO;
public static void SendMailKit(string[] tos)
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("發件人名稱", AppConfig.From));
foreach (var s in tos)
{
if (!string.IsNullOrWhiteSpace(s))
{
message.To.Add(new MailboxAddress("收件人名稱", s));
}
}
message.Subject = "郵件標題"; //郵件標題
var builder = new BodyBuilder
{
//TextBody = "Hey geffzhang<br>DennisDong"//不支持Html
HtmlBody = "Hey geffzhang<br>DennisDong"//支持Html
};
//添加附件
//builder.Attachments.Add($@"{Directory.GetCurrentDirectory()}\1.png");//包含圖片附件,或者正文中有圖片會被當成垃圾郵件退回,所以不建議放圖片內容(跟Mail類庫框架無關)
builder.Attachments.Add($@"{Directory.GetCurrentDirectory()}\ConsoleApp1.exe.config");
message.Body = builder.ToMessageBody();
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
var mSendMail = "XXX@163.com";
var mSendPwd = "XXXXX";//163和qq都是授權碼,不是郵箱密碼
client.Connect("smtp.163.com", 465, true);//網易、QQ支持 25(未加密),465和587(SSL加密)
client.Authenticate(mSendMail, mSendPwd);
try
{
client.Send(message);//發送郵件
client.Disconnect(true);
}
catch (SmtpCommandException ex)
{
Console.WriteLine(ex.ErrorCode);
}
catch (Exception ex)
{
throw ex;
}
}
}