通过ASP.Net跟踪退回的电子邮件

时间:2014-08-18 09:13:36

标签: asp.net email tracking smtpclient bouncedemail

我是否可以通过代码跟踪退回的电子邮件。

考虑电子邮件ID,例如'skdfisdcnsodioifs@gmail.com'。这是有效的电子邮件ID,但不存在,因此它肯定会被退回支持。

我正在使用ASP.Net的SmtpClient和“message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure”。但是它仍然显示成功发送的消息,在大约20-25分钟后,我收到了失败递送通知电子邮件。

以下是我的代码

class Program
{
    static void Main(string[] args)
    {
        SmtpClient client = new SmtpClient("xxxx.xxxxxx.xxx", 25);
        client.Credentials = new NetworkCredential("xxxxxx@xxxxxx.com", "xxxxxx");            
        MailAddress to = new MailAddress("abcdsfasdfasdfasdfasdf2342342defgh@gmail12333.com");
        MailAddress from = new MailAddress("xxxxxx@xxxxxx.com");            
        MailMessage message = new MailMessage(from, to);
        message.Headers.Add("Return-Path", "xxxxxx@gmail.com");
        message.ReplyToList.Add(new MailAddress("xxxxxx@gmail.com"));
        message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess;
        message.Subject = "Test POC";
        message.Body = "This is a test e-mail message sent by an application. ";            
        client.SendCompleted += client_SendCompleted;            
        string UserState = "test"; 
        client.Timeout = 3;
        try
        {
            Console.WriteLine("start to send email ...");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;                
            client.SendAsync(message,UserState);              
            Console.WriteLine("email was sent successfully!");
        }
        catch (SmtpFailedRecipientsException ep)
        {
            Console.WriteLine("failed to send email with the following error:");
            Console.WriteLine(ep.Message);
        } 
        catch (Exception ep)
        {
            Console.WriteLine("failed to send email with the following error:");
            Console.WriteLine(ep.Message);
        } 
        Console.WriteLine("test.");
        Console.ReadKey();
    }

    static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        // Get the unique identifier for this asynchronous operation.
        //String token = (string)e.UserState;
        if (e.Cancelled)
        {
            Console.WriteLine("Send canceled.");
        }
        if (e.Error != null)
        {
            Console.WriteLine("[{0}] ", e.Error.ToString());
        }
        else
        {
            Console.WriteLine("Message sent.");
        }        
    }
}

提前致谢。 阿布舍克巴克

0 个答案:

没有答案