Our application sends out emails using the
new SmtpClient(smtpServer).Send(message);
We are making sure that the smtpServer is valid, the message has To and From addresses, a subject and a body. If any of these are missing, we would log an exception before even attempting to send a message.
But the application frequently fails with the below exception.
Email Fail: System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: Too many invalid recipients.
Now, please note that the email send functionality is not failing always. For the same "To" address, it fails, may be about half the times. So, if the application tries sending out emails 100 times, we are getting some 40+ failures with the same message.
I have already validated that the To address and the From address exists. We are seeing this issue since last month when we shifted from Outlook to Gmail.
Here is the code.
if (fromAddress.Length == 0)
fromAddress = Resources.FromAddress;
if (toAddress.Length == 0) return "To Address is Required.";
if (smtpServer.Length == 0)
smtpServer = Resources.SMTPServer;
if (string.IsNullOrEmpty(smtpServer))
return "SMTP sever not specified";
MailMessage mailMessage = new MailMessage();
//set the addresses
mailMessage.From = new MailAddress(fromAddress);
string[] toAdds = toAddress.Split(';');
short i = 0;
foreach (string address in toAdds)
{
if(i==0) mailMessage.To.Add(address); else mailMessage.CC.Add(address);
i++;
}
if (!string.IsNullOrEmpty(bcc))
{
string[] bccAddresses = bcc.Split(';');
foreach (string address in bccAddresses)
{
mailMessage.Bcc.Add(address);
}
}
if (!string.IsNullOrEmpty(cc))
{
string[] ccAddresses = cc.Split(';');
foreach (string address in ccAddresses)
{
mailMessage.CC.Add(address);
}
}
if (subject.Length > 0)
mailMessage.Subject = subject;
mailMessage.Body = sBody;
mailMessage.IsBodyHtml = true;
SmtpClient emailClient = new SmtpClient(smtpServer);
emailClient.Send(mailMessage);
Any directions?
Talk1:
Feroz this can happen for a number of reasons.. if you have individual emails and one of them has an invalid hostname in their email address, or if you have not parsed the recipients list properly.. perhaps you have 2 email address colliding for example tomthumb@aol.commarrK@gmail.com and there is no proper separator.. the best thing I would suggest is to set break points process the large list and see which email(s) it fails on and inspect the value.. it's probably something so simple..
Talk2:
also without exposing the actual recipients email addresses to us could you give an exact example of the emails masking them or changing them ...
Talk3:
We have just one email address in "To" and one in "CC". Something like "axxxxs@txxxxk.com" in To and "axxxxk@rxxxxd.com" in CC and the From address is like Axxxxs@rxxxxd.com
Talk4:
This has become a difficult one to crack since all the emails are being sent to the same "To", same "CC" and from the same "From" addresses and some of them are failing. The angle of Outlook Vs Gmail is also interesting since the complaints started coming in at around the same time when the company shifted from Outlook to Gmail.
Talk5:
show the code of how you are sending multiple emails and how you are assigning it's delimited separator.. for example are you using , or ; for the email separator.. and also are you trimming or getting the substring position of the last email which you would not have to use the ending separator so it should looks something like this in your inspector "axxxxs@txxxxk.com;""axxxxk@rxxxxd.com" in the CC portion
Solutions1
If the same email with the same sender and recipients is sometimes accepted, sometimes rejected by the SMTP server, it may be the result of a server antispam policy. For example :
Directory Harvest Attack Prevention (DHAP) : which causes a "550 Too many invalid recipients" error when exceeding a number of RCPT TO commands over a given period of time.
Quotas : a limit on the number of mails that a mailbox/IP can send per minute/second to prevent spamming.
You can validate if the SMTP server settings are the cause of your problem by (temporarily) :
whitelisting the IP address of your SMTP client
disabling any quota/antispam Policy applied to your sender's mailbox
If that doesn't solve your problem, then use a tool like WireShark to record the SMTP dialog and check exactly what email addresses are sent in the RCPT TO command, and in which cases the SMTP Server rejects them. Then, post it here.