简体   繁体   English

如何检查电子邮件状态是否已在c#中删除

[英]How to check e-mail status is delievered or not in c#

Currently i am sending mail through SMTP Code is given below 目前我通过SMTP代码发送邮件如下

SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;
client.Send(msg);

I have to check the delievery status that mail is delievered or not using smtp exception using code 我必须使用代码检查邮件被提交或不使用smtp异常的delievery状态

While @Chase is technically correct, it's possible to get delivery status notifications if the SMTP servers along the route support this extension. 虽然@Chase在技术上是正确的,它可能得到传递状态通知,如果沿线支持SMTP服务器这个扩展。

I'm not aware of a way to do this with System.Net.Mail, but with MailKit , you can actually request delivery status notifications for your recipients. 我不知道如何使用System.Net.Mail执行此操作,但使用MailKit ,您实际上可以请求收件人的传递状态通知。

In MailKit, the first thing you'll need to do (until I figure out a nicer API for this) is to setup your own SmtpClient class like this: 在MailKit中,你需要做的第一件事(直到我找到一个更好的API)是设置你自己的SmtpClient类,如下所示:

class MySmtpClient : SmtpClient
{
    public MySmtpClient ()
    {
    }

    /// <summary>
    /// Get the envelope identifier to be used with delivery status notifications.
    /// </summary>
    /// <remarks>
    /// <para>The envelope identifier, if non-empty, is useful in determining which message
    /// a delivery status notification was issued for.</para>
    /// <para>The envelope identifier should be unique and may be up to 100 characters in
    /// length, but must consist only of printable ASCII characters and no white space.</para>
    /// <para>For more information, see rfc3461, section 4.4.</para>
    /// </remarks>
    /// <returns>The envelope identifier.</returns>
    /// <param name="message">The message.</param>
    protected override string GetEnvelopeId (MimeMessage message)
    {
        // The Message-Id header is probably the easiest way to go...
        return message.MessageId;
    }

    /// <summary>
    /// Get the types of delivery status notification desired for the specified recipient mailbox.
    /// </summary>
    /// <remarks>
    /// Gets the types of delivery status notification desired for the specified recipient mailbox.
    /// </remarks>
    /// <returns>The desired delivery status notification type.</returns>
    /// <param name="message">The message being sent.</param>
    /// <param name="mailbox">The mailbox.</param>
    protected override DeliveryStatusNotification? GetDeliveryStatusNotifications (MimeMessage message, MailboxAddress mailbox)
    {
        // Since you want to know whether the message got delivered or not,
        // you'll probably want to get notifications for Success and Failure.
        return DeliveryStatusNotification.Success | DeliveryStatusNotification.Failure;
    }
}

Then, once you have that, you'd use it like this: 然后,一旦你有了,你就像这样使用它:

using (var client = new MySmtpClient ()) {
    client.Connect ("smtp.gmail.com", 465, true);
    client.Authenticate ("username", "password");
    client.Send (message);
    client.Disconnect (true);
}

What will now happen is that whenever there is a success or failure notification, you'll get an email delivered to your account's Inbox (POP3 or IMAP depending on what you use) that will contain a multipart/report MIME part which will typically contain 3 other MIME parts: a human readable explanation, a message/delivery-status MIME part which will have a body that contains some key/value pairs and a third part which contains the original message (or sometimes just the headers). 现在将发生的是,无论何时成功或失败通知,您都会收到一封电子邮件,该电子邮件将发送到您的帐户的收件箱(POP3或IMAP,具体取决于您使用的内容),其中包含multipart/report MIME部分,通常包含3部分其他MIME部分:人类可读的解释, message/delivery-status MIME部分,其中包含一些包含一些键/值对的主体,第三部分包含原始消息(或者有时只包含标题)。

Currently, MailKit does not have a special MIME class for dealing with message/delivery-status MIME parts, but you can work around this by parsing the content like this: 目前,MailKit没有用于处理message/delivery-status MIME部分的特殊MIME类,但您可以通过解析这样的内容来解决此问题:

var mds = message.BodyParts.OfType<MimePart>.Where (x => x.ContentType.Matches ("message", "delivery-status")).FirstOrDefault ();
if (mds != null) {
    using (var memory = new MemoryStream ()) {
        mds.ContentObject.DecodeTo (memory);
        memory.Position = 0;

        // the content of a message/delivery-status MIME part is a
        // collection of header groups. The first group of headers
        // will contain the per-message status headers while each
        // group after that will contain status headers for a
        // particular recipient.
        var groups = new List<HeaderList> ();
        while (memory.Position < memory.Length)
            groups.Add (HeaderList.Load (memory));

        // TODO: take a look at the specific "headers" to get the info we 
        // care about. For more info on what these header field names and
        // values are, take a look at https://tools.ietf.org/html/rfc3464
    }
}

Update: I've added a MessageDeliveryStatus class to MimeKit that handles parsing the content of message/delivery-status MIME parts for you, but it may take up to 2 weeks before I make another release since I just made a release 2 days ago. 更新:我已经为MimeKit添加了一个MessageDeliveryStatus类,用于处理解析message/delivery-status MIME部分的内容,但是我可能需要长达2周才能发布另一个版本,因为我刚刚在2天前发布了一个版本。 Expect to find this new class in MimeKit 1.2.8 when I do release it. 当我发布它时,期望在MimeKit 1.2.8中找到这个新类。

You can't check it. 你无法检查它。 Because you use SMTP , it's impossible to tell whether delivery succeeded or not. 因为您使用SMTP ,所以无法判断交付是否成功。 Mail is routed while being delivered. 邮件在发送时被路由。 Some useful tricks are verify the email address is valid before you send it and set up a single no reply address as an actual inbox and then go into the email account using POP3 and look for bounce back messages. 一些有用的技巧是在发送电子邮件地址之前验证电子邮件地址是否有效,并将单个无回复地址设置为实际收件箱,然后使用POP3进入电子邮件帐户并查找退回邮件。 Details how SMTP works 详细说明SMTP的工作原理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM