我想在发送邮件是成功还是失败后获得递送通知,在谷歌上我了解了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);
}
}
}
}
在输出上,即使来自电子邮件是错误的,我也取得了成功。有人可以帮我解释如何实现这种情况。
谢谢, 什雷亚