How to check MailMessage was delivered in .NET?

早过忘川 提交于 2019-11-28 13:03:17

No, there is no reliable way to find out if a message was indeed delivered.

Doing so will require access to the end SMTP server for every person you are emailing.

If you do not get an exception, you can assume that the SMTP server did its best to deliver the email.

There's no way to be 100% sure that a mail message has been received when sent via SmtpClient due to the way email works. The fact that SmtpClient doesn't throw an exception essentially means that you've done everything right, but a failure can happen further down the line, for example:

  • The receiving mail server could reject the mail
  • An intermediate mail server could reject the mail
  • The server that SmtpClient is transmitting mail through could decide to refuse to transmit the mail

One solution you could use is to create an httphandler for your website images. If you send an HTML message which includes at least 1 image, then you could embed querystring data to the end of that image. This could even be something like a 1x1 transparent image. When the user reads the email, this sends the request to the server to fetch the image data, and in turn, you could capture that request and denote that the message was read.

This is not bulletproof however, because most email clients block images by default unless the user specifies they would like to view images in the email.

If the recipient e-mail address is valid you don't get an immediate return value about the successful delivery of the message; see the signature:

public void Send(MailMessage message)

The SMTP server will notify the sender (or whoever you specify for the notification) almost immediately with an 'Undeliverable' notification whenever the recipient e-mail address is invalid/fake.

SMTP servers are required to periodically retry delivery. When the recipient e-mail address is a valid address but for some reason the SMTP server could not deliver the message, the SMTP server will return a failure message to the sender if it cannot deliver the message after a certain period of time.

RFC 2821 contains more details.

From section 2.1 Basic Structure

In other words, message transfer can occur in a single connection between the original SMTP-sender and the final SMTP-recipient, or can occur in a series of hops through intermediary systems. In either case, a formal handoff of responsibility for the message occurs: the protocol requires that a server accept responsibility for either delivering a message or properly reporting the failure to do so.

See sections 4.5.4 and 4.5.5

From section 6.1 Reliable Delivery and Replies by Email

If there is a delivery failure after acceptance of a message, the receiver-SMTP MUST formulate and mail a notification message. This notification MUST be sent using a null ("<>") reverse path in the envelope. The recipient of this notification MUST be the address from the envelope return path (or the Return-Path: line).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!