0

I have one "Email" function in my project.if all email ids are correct in same domain , email will send successfully. if one of the email is wrong then email will fail and get below error.

Exception:-The server rejected one or more recipient addresses. The server response was: 550 5.0.0 Requested action not taken: mailbox unavailable or not local.  , InnerException:-System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: The server rejected one or more recipient addresses. The server response was: 550 5.0.0 Requested action not taken: mailbox unavailable or not local.       --- End of inner exception stack trace ---     at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)     at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)     at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) , Stack Trace :-   at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)     at System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message)     at System.Web.Mail.SmtpMail.Send(MailMessage message)     at Entity.Common.Mail.SendMail(String mailFrom, String mailTo, String mailCc, String Subject, String Content, String Bcc, String optionalfrom)

code follows

MailAddress xfrom = new MailAddress(EmailFrom);
        MailMessage mm = new MailMessage();
        mm.Sender = xfrom;
        mm.From = xfrom;

        //to
        string[] tos = EmailTO.Trim().Split(';');
        for (int i = 0; i < tos.Length; i++)
        {
            mm.To.Add(new MailAddress(tos[i]));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = smtpServer;
        smtp.Credentials = new NetworkCredential(smtpMailAccount, smtpMailPassword);
        smtp.Send(mm);
        IsSuccess = true;

How to ignore the wrong email id and send email to rest all correct email ids ? i want to send email even wrong email id is exists. how to solve this issue? help / suggestion appreciated.

nichu09
  • 882
  • 2
  • 15
  • 44

2 Answers2

1

Catch the exception as a SmtpFailedRecipientException, which has a property FailedRecipient. Remove this one from the To collection, and retry until no more SmtpFailedRecipientException is thrown.

A small excerpt of a project where I did just that, note that there is also a plural version of that exception, which can contain more than one failed recipient in its InnerExceptions list:

try
{
    /* code to send */
}
catch (SmtpFailedRecipientsException recsex)
{
    var failedRecipient = String.Empty;
    foreach (var rex in recsex.InnerExceptions)
    {
        /* rex.FailedRecipient contains the invalid address to remove
         * before the next attempt.
         * note that you might end up with an empty recipient list
         * in case all were invalid, then you should also no longer
         * attemtp to resubmit the mail */
    }
}
catch (SmtpFailedRecipientException recex)
{
    /* recex.FailedRecipient contains the invalid address to remove
     * before the next attempt */
}
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
0

I don't know what an Email id is.

But if it is your goal is to Ignore some emails, you could do an try catch around it.

try {
    //yourcode
} catch (Exception e)
{
    if (/*Check if e is not equal to "mailbox unavailable or not local"*/) throw; //beacuse its an other error
    //your wrong email "id" error gets ignored

}

EDIT:

You could check before you send the Email if the reciepents exists. Take a look at the STMP VRFY command.

  • 1
    they seem to have a list of multiple recipients per mail, and want that mail to go out to the valid recipients even if one or more of them are invalid. of course it could be another approach to send one mail per recipient, ignoring exceptions as you suggest, but this has two consequences: 1) the other recipients would not see their co-recipients on the mail and 2) more smtp traffic. – Cee McSharpface Jun 26 '18 at 09:39