通过 SSL 执行 SMTP 有两种方法:显式和隐式。显式意味着你以明文连接到一个普通的 SMTP 端口(通常是 25 或 587),然后发出“starttls”命令来切换到 SSL 模式。隐式意味着您连接到一个期望所有内容都是 SSL 的端口(通常是 465)。
Asp.net 使用“System.Net.Mail.SmtpClient()”发送电子邮件。主要问题是 SmtpClient 不支持隐式 SSL 连接,但支持显式连接(使用 SSL 的 System.Net.Mail 对端口 465 进行身份验证)。因此,如果邮件服务器(SMTP)不支持显式连接,则无法发送电子邮件并显示“连接超时”、“无法将消息发送到 SMTP 服务器。传输错误代码为 0x80040217。服务器响应不可用”等。
为了在 ASP.net 中解决这个问题,我们可以使用 Windows 2000 库 (Cdosys.dll) 的协作数据对象 (CDO) 来发送带有附件的电子邮件消息。 Microsoft Outlook 使用此 DLL 发送电子邮件。在您的 ASP.net 解决方案中,您必须添加参考“Microsoft CDO for windows 2000 Library”。它将在 Bin 文件夹中添加两个标记的 dll。
现在在 C#.net 中执行以下代码:
public static void SendMail(string FromName, string FromEmail, string ReceiverEmail, string CC, string BCC, string subj, string Mssg)
{
const var cdoSendUsingPort = 2;
const var cdoBasicAuth = 1;
const var cdoTimeout = 60;
var mailServer = "mail.XXXXXXX.net";
var SMTPport = 465;
var mailusername = "yyy@XXXXXXX.net";
var mailpassword = "PPPPXXXX";
var objEmail = CreateObject("CDO.Message");
var objConf = objEmail.Configuration;
var objFlds = objConf.Fields;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername;
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword;
objFlds.Update();
objEmail.To = ReceiverEmail;
objEmail.From = FromEmail;
objEmail.CC = CC;
objEmail.BCC = BCC;
objEmail.Subject = subj;
objEmail.HTMLBody = Mssg;
objEmail.Send();
}
在 VB.net 中
Public Shared Sub SendMail(ByVal FromName As String, ByVal FromEmail As String, ByVal ReceiverEmail As String, ByVal CC As String, ByVal BCC As String, ByVal subj As String, ByVal Mssg As String)
''#################Sending Email##########################
Const cdoSendUsingPort = 2 ' Send the message using SMTP
Const cdoBasicAuth = 1 ' Clear-text authentication
Const cdoTimeout = 60 ' Timeout for SMTP in seconds
Dim mailServer = "mail.XXXXXXX.net"
Dim SMTPport = 465
Dim mailusername = "yyy@XXXXXXX.net"
Dim mailpassword = "PPPPXXXX"
Dim objEmail = CreateObject("CDO.Message")
Dim objConf = objEmail.Configuration
Dim objFlds = objConf.Fields
With objFlds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword
.Update()
End With
objEmail.To = ReceiverEmail
objEmail.From = FromEmail
objEmail.CC = CC
objEmail.BCC = BCC
objEmail.Subject = subj
objEmail.HTMLBody = Mssg
'objEmail.AddAttachment "C:\report.pdf"
objEmail.Send()
End Sub
参考:
Original post
隐式和显式 SMTP http://help.fogcreek.com/9002/using-an-smtp-server-with-ssl
使用 Cdosys.dll 库发送带有附件https://support.microsoft.com/en-us/help/310212/how-to-use-the-cdosys-dll-library-to-send-an-e-mail-message-with-attac的电子邮件