GMail SMTP via C# .Net errors on all ports

前端 未结 7 1606
鱼传尺愫
鱼传尺愫 2020-11-27 17:35

I\'ve been trying for a whlie on this, and have so far been failing miserably. My most recent attempt was lifted from this stack code here: Sending email through Gmail SMT

7条回答
  •  误落风尘
    2020-11-27 17:41

    There are two ways to do SMTP over SSL: Explicit and Implicit. Explicit means you connect to a normal SMTP port (usually 25 or 587) in plaintext, then issue the “starttls” command to switch to SSL-mode. Implicit means you connect to a port that expects everything to be SSL (usually 465).

    Asp.net use “System.Net.Mail.SmtpClient()” to send Email. The main problem is SmtpClient does not support implicit SSL connections, but does support explicit connections (System.Net.Mail with SSL to authenticate against port 465). So, if the mail server(SMTP) do not support Explicit connection, it fails to send email and show messages like “Connection timeout“, “The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available” etc.

    To solve this issue in ASP.net we can use the Collaboration Data Objects (CDO) for Windows 2000 library (Cdosys.dll) to send an e-mail message with attachments. Microsoft Outlook use this DLL to send email. In your ASP.net solution, you have to add referance “Microsoft CDO for windows 2000 Library“. It will add two marked dll in Bin folder.

    Now do the bellow code in 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();
    }
    

    In 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
    

    Referance: Original post Implicit & Explicit SMTP http://help.fogcreek.com/9002/using-an-smtp-server-with-ssl use the Cdosys.dll library to send an e-mail message with attachments https://support.microsoft.com/en-us/help/310212/how-to-use-the-cdosys-dll-library-to-send-an-e-mail-message-with-attac

提交回复
热议问题