使用 gmail smtp 服务器的 smtp 送达回执

问题描述

我想在发送邮件后收到送达通知,无论是成功还是失败,在谷歌上我了解了 DeliveryNotificationoptions,但是当我尝试创建一个 poc 来检查这个时,我没有得到正确的回答。

 class Program
    {
        static string smtpAddress = "smtp.gmail.com";
        static int portNumber = 587;
        static bool enableSSL = true;
        static string emailFromAddress = "MyId@gmail.com"; //Sender Email Address  
        static string password = "password"; //Sender Password  
        static string emailToAddress = "SomeEmailToTest@gmail.com"; //Receiver Email Address  
        static string subject = "Hello";
        static string body = "Hello,This is Email sending test using gmail.";
        static void Main(string[] args)
        {
            SendEmail();
        }
        public static void SendEmail()
        {
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFromAddress);
                mail.To.Add(emailToAddress);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment  
                using (SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFromAddress,password);
                    smtp.EnableSsl = enableSSL;

                    mail.Headers.Add("disposition-Notification-To",emailToAddress);

                    mail.DeliveryNotificationoptions = DeliveryNotificationoptions.OnSuccess;
                    smtp.Send(mail);
                  
                }
            }
        }
    }

输出时,即使发件人电子邮件错误,我也取得了成功。有人可以帮我解释一下如何实现这种情况。

谢谢,Shreya

解决方法

所以我没有找到这个特定场景的答案,但找到了一些不同的解决方法

 class Program
    {
        static string smtpAddress = "smtp.gmail.com";
    static int portNumber = 587;
    static bool enableSSL = true;
    static string emailFromAddress = "MyId@gmail.com"; //Sender Email Address  
    static string password = "password"; //Sender Password  
    static string emailToAddress = "SomeEmailToTest@gmail.com"; //Receiver Email Address  
    static string subject = "Hello";
    static string body = "Hello,This is Email sending test using gmail.";
        static void Main(string[] args)
        {
            testValidEmail();
        }
 public static void testValidEmail()
        {
            TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com",25);
            string CRLF = "\r\n";
            byte[] dataBuffer;
            string ResponseString;
            NetworkStream netStream = tClient.GetStream();
            StreamReader reader = new StreamReader(netStream);
            ResponseString = reader.ReadLine();
            /* Perform HELO to SMTP Server and get Response */
            dataBuffer = BytesFromString("HELO Shreya" + CRLF);
            netStream.Write(dataBuffer,dataBuffer.Length);
            ResponseString = reader.ReadLine();
            dataBuffer = BytesFromString("MAIL FROM:<MyEmailID>" + CRLF);
            netStream.Write(dataBuffer,dataBuffer.Length);
            ResponseString = reader.ReadLine();
            /* Read Response of the RCPT TO Message to know from google if it exist or not */
            dataBuffer = BytesFromString("RCPT TO:<" + "TESTEmailID" + ">" + CRLF);
            netStream.Write(dataBuffer,dataBuffer.Length);
            ResponseString = reader.ReadLine();
            if (GetResponseCode(ResponseString) == 550)
            {
                Console.Write("Mai Address Does not Exist !<br/><br/>");
                Console.Write("<B><font color='red'>Original Error from Smtp Server :</font></b>" + ResponseString);
            }
            /* QUITE CONNECTION */
            dataBuffer = BytesFromString("QUITE" + CRLF);
            netStream.Write(dataBuffer,dataBuffer.Length);
            tClient.Close();
        }
        private static  byte[] BytesFromString(string str)
        {
            return Encoding.ASCII.GetBytes(str);
        }

        public static  int GetResponseCode(string ResponseString)
        {
            return int.Parse(ResponseString.Substring(0,3));
        }
       }
}
,

我还使用 ASPNETMX 找到了不同的解决方法,您可以在该站点上找到许可证密钥、代码示例和 Dll

class Program
    {
        static string smtpAddress = "smtp.gmail.com";
        static int portNumber = 587;
        static bool enableSSL = true;
        static string emailFromAddress = "MYID"; //Sender Email Address  
        static string password = "password"; //Sender Password  
        static string emailToAddress = "RecieverID"; //Receiver Email Address  
        static string subject = "Hello";
        static string body = "Hello,This is Email sending test using gmail.";
         static void Main(string[] args)
        {
            SendEmail();
        }
public static void SendEmail()
        {
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFromAddress);
                mail.To.Add(emailToAddress);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment  
                using (SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFromAddress,password);
                    smtp.EnableSsl = enableSSL;

                    mail.Headers.Add("Disposition-Notification-To",emailToAddress);

                    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                    try {
                     
                        MXValidate.LoadLicenseKey("aaa-bbb-ccc");
                        aspNetMX.MXValidate mx = new aspNetMX.MXValidate();

                        // your server infos
                        mx.SMTPHello = "smtp.gmail.com";
                        mx.SMTPFrom = emailFromAddress;
                        mx.LogInMemory = true;

                        mx.RecurseMailDomains = true;

                        MXValidateLevel level = mx.Validate("randomtest@gmail.com");


                        //aspNetMX.MXValidateLevel level = mx.Validate("valiemailToTest@gmail.com",aspNetMX.MXValidateLevel.Mailbox);

                        if (level == aspNetMX.MXValidateLevel.Mailbox)
                        {
                            // Valid email address.
                        }
                        else
                        {
                            // NOT valid email address.
                        }
                        smtp.Send(mail);
                    
                    }
                    catch (SmtpFailedRecipientsException ex) {
                        for (int i = 0; i < ex.InnerExceptions.Length; i++)
                        {
                            SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
                            if (status == SmtpStatusCode.MailboxBusy ||
                                status == SmtpStatusCode.MailboxUnavailable)
                            {
                                Console.WriteLine("Delivery failed - retrying in 5 seconds.");
                                System.Threading.Thread.Sleep(5000);
                                smtp.Send(mail);
                            }
                            else
                            {
                                Console.WriteLine("Failed to deliver message to {0}",ex.InnerExceptions[i].FailedRecipient);
                            }
                        }
                    }
                    catch (SmtpException e)
                    {
                        Console.WriteLine("Error: {0}",e.StatusCode);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception caught in RetryIfBusy(): {0}",ex.ToString());
                    }
                    Console.WriteLine(mail.DeliveryNotificationOptions);
                }
            }
        }
   }

}