send email to many recipients in C# check which one fail - c#

References to this link, I understand how to send email to many recipients my question is if one of them fail this mean Send(Msg) will throw error for all ? or just for specific recipient appreciate any help thanks

Assuming you are using System.Net.Mail.SmtpClient.Send(MailMessage), the documentation shows that it can throw a "SmtpFailedRecipientsException" exception when the message could not be delivered to one or more of the recipients in MailMessage.To, MailMessage.CC, or MailMessage.Bcc.
The SmtpFailedRecipientsException exception has an InnerExceptions property.
Gets one or more SmtpFailedRecipientExceptions that indicate the e-mail recipients with SMTP delivery errors.
https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx
Note: Depending on the issue, the SMTP server may accept the message and fail to deliver it to the recipient further along in the process.

Related

System.Net.Mail.SmtpFailedRecipientException for inexistent mail address c# asp.net

I use the smtp for sending mail from my application , I want to catch the error when the mail address is valid but inexistent but the method didn't catch even i implement this code:
catch (System.Exception exp)
{
if (exp.GetType() == typeof(SmtpFailedRecipientException))
{
//// display error message
}
}
What should i do?? Is SmtpFailedRecipientException didn't recognize inexistent address mail??
SmtpFailedRecipientException Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation to a particular recipient. MS Link
I assume it's an external SMTP client , so when sending the mail
,it will not validate external mail address immediately. It is later after
the external node send back error message will it popup message sending
fail error. The SmtpClient component will close the communication with
server right after the message has been sent out, but not trace the
sequential message delivery status. MS Answer
In order to work fix your problem there are couple of things you can do :
Try an external library Here's a link for a good one
Create your own SMTP socket program Here's another link

How to handle bounce email messages when you send emails on behalf of customer

I need advice on processing/tracking bounce emails.
We have a scenario, where we need to send emails to recipient on behalf of our customers.
lets say we need to send email to user#recipient.com with from as emailer#cusotmer.com but when this email fails we need to receive the bounce/failure notification on mybouncetracker#mydomain.com
I tried using the reply-to/return-path but both getting replaced with emailer#cusotmer.com.
We were not able to process bounce message as we dont own the inbox for emailer#cusotmer.com.
Please help!!..
We created smtp client on C#.net
Please be advised that we can't rely on the reply-to headers or inserting a custom ID to our original message's headers since the bounced messages returned by the mail servers vary from server to server. Many of them just bounced with a plain-text saying that the delivery is failed without including the original message.
My advise is to use Regex with compiled options (for performance) to scan the messages.
After you have read the message content, you can use the following code to classify a message as a soft bounce that states that the message sending was failed:
string pattern = "failed after \S+ sent[\s\n]the[\s\n]message";
Regex regexPattern = new Regex(pattern, RegexOptions.Compiled);
if (regexPattern.Match(myMessage.BodyHtml))
{
Console.WriteLine("This is a soft bounce");
}
You can add more patterns to classify more messages.

Throwing Exception while sending Email to multiple recipients using smtp client

I have an application which uses SmtpClient to send an email. I am trying to send an email to multiple recipients. I have two recipients in my to list e.g "aman#gmail.com,abc#xyz.com". and I am trying to send the email to this list but my application is throwing the exception as below:
Client does not have permission to submit mail to this server. The server response was: 4.7.1 (abc#xyz.com): Relay access denied.
because of this aman#gmail.com is also not able to receive the email.
I need to implement the functionality that even there is an invalid address like abc#xyz.com in the ToList, an email should be sent successfully to aman#gmail.com.
Can anybody please help me in this?
Does this error message come from your own email server, or from that of xyz.com? I'm guessing it's your own server, and that you either need to aunthenticate before sending, or use your own email address for sending (but the latter is kind of a long shot -- "we do not relay" means a server which is neither the sender's or the recipient's refuses to act as a middleman). It is also possible that the mail exchanger for xyz.com is misconfigured (either the MX record in DNS points to the wrong server, or the admin failed to configure it to accept this responsibility - technically basically the same thing) or that your client somehow ends up connecting to the wrong place.
(Not a proper answer but this got too long to fit in a comment.)

SmtpClient get result from server on send

The SmtpClient send method returns void. Is there any way to get the server response? Do I just assume it was successful unless it throws an exception?
The class I'm referring to... http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
To answer your second point, yes, all you can do is assume it's successful - meaning it got the message to the server and the server accepted it, unless you get an exception.
You probably already know the rest of this, but just in case...
From there, the email could get lost and not delivered any number of ways. Your server may accept it and decide not to send it, or accept it and lose power before crashing. It may get blocked by a spam filter along the way, etc.
You can think of an email as being similar to a regular piece of mail in that it passes through several hands between the sender and the recipient. From your code, you can only confirm that it got to the SMTP server you're using to send, which is similar to handing it to a teller at the post office. You don't know (or need to know) how the message is routed from there. it could be by air, ground, or carrier pigeon. You're out of the equation - you don't need to know how it gets sent, just that you trust that they know how to send it. (The same can be said for an email.)
If you need to confirm that the recipient opened it, there are ways of embedding an image in an HTML message on your server and tracking in your logs when that image is accessed, etc. (Google email tracking and email open tracking)
On the other hand...
If the server rejects it, then you do get a server response in a manner of speaking - there should be an error code and a description in the error, which you can use to troubleshoot why it didn't make it, or use error handling to try another route, etc.
You can utilize SendCompleted Event to check that your smtpclient works fine like this:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendcompleted.aspx
But you cannot get confirmation that your message reached recipient because it may stuck in any server/filter in message chain.
You assume that it was successful unless it throws... although success in this case only means that it was accepted by the mail server, anything else is then up to the server...
IF you want a little bit of control you can use SendAsync and hook the SendCompleted event...

SMTP Error Codes and determining if email is still delivered

I am using System.Net.Mail to to send mail. I do not know the type/version of the SMTP relay that it will connect to.
Some errors will result in no email being sent (e.g. no addresses or invalid from address) whilst others errors will still result in an email being sent.
E.g. Send To : bob#somewhere.com CC: fred#somewhere.com and DoesNotExist#somewhere.com may result in an error of
The server response was: 550 #5.1.0 Address rejected DoesNotExist#somewhere.com.
But the email still appears to be delivered to Bob and Fred.
Is there any reference to which error codes will still result in an email being delivered or any programmatic way of determining this?
This is the way that SMTP operates, as described in the RFC.
Failure to deliver to one recipient does not affect delivery to other recipients.
Generically, codes starting with 4 and 5 are failures, codes starting with 2 are success codes - see RFC821 section "4.2.2. NUMERIC ORDER LIST OF REPLY CODES".
"Email being delivered" is hard to define. Email being accepted for delivery by the SMTP server is defined by the SMTP protocol (and the server will signal acceptance to deliver a message) but this server might just relay the message to another server, or operate in a smart host configuration where it just accepts messages and passes them on to the smart host - delivery is another thing and it's usually associated with local delivery (LMTP).
Some mail clients work around this problem of not being able to tell if a message has been delivered by implementing the dreaded read receipts - but this implementation is entirely on the client side and AFAIK it has nothing to to with SMTP.
RFC 821 is the one that describes SMTP and includes information about different types of errors, but not sure if it contains the details you're looking for. And either way, even if you can find out that the mail server accepted the email for some users, that is not the same as saying that it was delivered to them.
As far as I know, there is no way of telling if an email has been delivered except if the recipients mail client will tell you in some way.
Non-delivery reports (NDRs) are system messages that report the delivery status of a message to the sender. The messages are a subclass of a general message information structure that is referred to as delivery status notifications. Delivery status notifications describe three different types of situations:
* Success (2.X.X numeric codes)
* Persistent transient failure (4.X.X numeric codes)
* Permanent failures (5.X.X numeric codes)
To learn more about delivery status notifications, see Request for Comment (RFC) 1891 and RFC 1893.
Qouted from Microsoft Support http://support.microsoft.com/kb/284204

Categories