15

Below code is workin fine . However I need get Failure or Success Notification to Specific address ([email protected]). But I'm receiving Delivery Notification mail to FromMail address([email protected]). Can you please help me to resolve this problem?

SmtpClient smtpClient = new SmtpClient();

MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("[email protected]", "BALA");
MailAddress adminAddress = new MailAddress("[email protected]");

smtpClient.Host = "Mail Server Name";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true; 

message.From = fromAddress;
message.To.Add(_sendTo); //Recipent email
message.Subject = _subject;
message.Body = _details;
message.IsBodyHtml = true;


message.Headers.Add("Disposition-Notification-To", "[email protected]");

message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

message.ReplyTo = adminAddress;

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
1
  • To get Failure or Success Notification try bitwise combination: message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess|DeliveryNotificationOptions.OnFailure; To get the notification to specific address try the message.ReplyToList property to indicate the list of addresses other than the From address to use to reply to the message.
    – Only You
    Apr 14, 2014 at 19:14

3 Answers 3

14

The short answer is what you are asking cannot be done in the direct manner in which you are assuming.

This will only work in certain conditions. The easiest to describe would be if the SMTP server you are using to send the message, is the same server that hosts the domain of the recipient email messages (the server you refer to when setting your .HOST property of smtpClient). So, if you were only sending to recipients on your local SMTP mail server, then this might work pretty reliably. But that depends on the specific SMTP server software being used and potentially also on how it is configured.

To explain why this is, you must realize that only the last SMTP mail server receiving the message that actually hosts the desired email addresses, will be able to authoritatively answer the question, is this a valid email address. If the message has to pass through any other email servers on the way to getting at this final authoritative server, the message has to be handed off sequentially from one server to the next server in the chain until it reaches that final authoritative server. This means that there is not a guaranteed method for authenticating a specific address. Couple this with the fact that some domains are configured to act as a black hole and swallow illegitimately addressed mail, and you can see that there are many reasons why you cannot rely on that methodology.

So, many messages to external domains are going to have to hit at least one separate SMTP server and depending on how that server answers or forwards the mail, it will determine the results for any specific receiving domain. In fact, monitoring the FROM address for bounced messages is not foolproof either as my previous comment about some hosts putting some messages into a black hole if they do not appear to be valid.

2

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).

0

According to MSDN the .Send will throw a SmtpFailedRecipientsException EDIT: if the MESSAGE can not be delivered to one or more of the recipients. You can find the information on which one in the Failed Recipient property in the exception. Thus if you try and catch that exception and validate the address you're looking for in the Exception, that might help.

1
  • 3
    Please reread the doc : MSDN is not speaking about "found or not". The Send method does not check is addresses exist or not. It just check if addresses are valid (syntaxicaly speaking). The Send method can not guarantee distribution, so using SmtpFailedRecipientsException is not representative at all of a distribution success or failure.
    – JYL
    Feb 6, 2017 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.