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?
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.
Related
We are trying to send mail through SMTP setup as below
SmtpClient smtpClient = new SmtpClient(SMTPServer, SMTPPort);
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(SMTPUserName, SMTPassword);
MailAddress mailAddress = null;
if (!string.IsNullOrEmpty(sFromEmail))
mailAddress = new MailAddress(sFromEmail);
else
mailAddress = new MailAddress(SMTPEmailID);
MailMessage mailMessage = new MailMessage();
mailMessage.From = mailAddress;
mailMessage.Subject = sEmailSubject;
foreach (string toAddress in sToEmail.Split(';'))
{
if (toAddress.Trim() != string.Empty)
mailMessage.To.Add(toAddress);
}
mailMessage.IsBodyHtml = true;
mailMessage.Body = sEmailBody;
smtpClient.Send(mailMessage);
logText.Add(Environment.NewLine);
logText.Add("Mail sent successfully at " + DateTime.Now.ToString());
logCreation.createLogFileFromList(logText);
logText.Clear();
sErrorMessage = "Mail sent successfully";
but this end up with error
Mailbox unavailable. The server response was: 5.7.54 SMTP; Unable to
relay recipient in non-accepted domain
any setup I am missing here..
First
Email gateway is a SMTP server in charge of filter virus or spam. Email Gateway user maybe included spam list check this
Second
If you change your mail server ip address,your domain system may not have generalization dns in your exchange system.
Third
If you change your mail server ip address,your domain system may not have generalization your exchange system. You must be force generelization dns.
At this point can you check your project in running server
open powershell and edit your email info then run this query
send-mailmessage -to '<test#test.com>' -From '<admin#test.com>' -subject "TEST" -body "hi" -smtpServer mail.yourlocalsmtpdns.com
if you take this error change mail.yourlocalsmtpdns.com to your smtp ip address
if you not take error. You can think that your problem cause smtp domain address.
Fast solution,
you can connect to smtp with smtp ip address to your project.
I am sending emails via exchange web services (EWS C#), modern authentication and Exchange Online.
This works fine for all email addresses tested except one. This one email address is the main SMTP address of a user in our organization.
When I try to send the email it results in an error
"Microsoft.Exchange.WebServices.Data.ServiceResponseException: At least one recipient is not valid., Recipient 'xxxx.xxxx#yyy.com ' is not resolved. All recipients must be resolved before a message can be submitted."
The mail then resides in the draft folder of the sending mailbox.
If I view it there with Outlook then it shows 2-3 seconds the entered SMTP email address of the user and then it switches to this underlined resolved name view in Outlook.
Is there a way in EWS to trigger this resolve process manually? Or do I need a setting on the server?
Or am I missing something?
Thanks.
This is my code:
log.Debug("create email object");
EmailMessage message = new EmailMessage(service);
// Set properties on the email message.
message.Subject = subject;
message.Body = body;
message.From = from;
foreach (string emailReplyTo in replyTo)
{
message.ReplyTo.Add(emailReplyTo);
}
foreach (string emailTo in recipients)
{
message.ToRecipients.Add(emailTo);
}
if (cc != null)
{
foreach (string emailTo in cc)
{
message.CcRecipients.Add(emailTo);
}
}
if (bcc != null)
{
foreach (string emailTo in bcc)
{
message.BccRecipients.Add(emailTo);
}
}
if (attachmentData != null && !String.IsNullOrWhiteSpace(attachmentFileName))
{
log.Debug("Add attachment");
message.Attachments.AddFileAttachment(attachmentFileName, attachmentData);
}
log.Debug("Send and save copy of email");
message.SendAndSaveCopy();
log.Debug("Successfully sent email");
return true;
Funny thing. The issue was that the email address had a trailing blank.
The Exchange server is still able to resolve when viewing it in Outlook (it just takes some seconds) but in the code it creates the issue.
I am trying to send an email to a fake email address and get a SmtpFailedRecipientException, however when I send the email to a fake address on my company domain, no exception is thrown. I am developing in VS2015 with .NET 4.5.2 and running the console application on a Windows 7 environment.
This code works correctly and sends emails when the email addresses are valid, and it will even throw a SmtpFailedRecipientException when I try sending to an invalid gmail or hotmail address. It seems that the only time an exception isn't thrown is when I try to send to a fake address within my company domain. Our company uses Office365 and I get an 'Undeliverable' message in my inbox when I try to send to the fake address from my address.
Does anyone have an idea why this is happening? I suspect it might be a setting on the mail server, but I dont have access to the mail server to check and see. I also might just be missing something obvious and be making a rookie mistake.
// Initialize SMTP client
SmtpClient client = null;
try
{
string from = "myEmailAddress#mycompany.com", to = "fakeEmailAddress#mycompany.com";
// Build client
client = new SmtpClient("mail.mycompany.com");
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Create message
using (MailMessage message = new MailMessage(from, to)
{
Subject = "Test email to fake address",
Body = "This email won't be sent"
})
{
// Send message
client.Send(message);
}
}
catch (SmtpFailedRecipientException ex)
{
Console.WriteLine($"SmtpFailedRecipientException caught.{Environment.NewLine}{ex.ToString()}");
}
catch (Exception ex)
{
Console.WriteLine($"Exception caught.{Environment.NewLine}{ex.ToString()}");
}
I have create function to send an email. This function was work successful on localhost but on server its failed without any exception. I know the problem comes from my Port on IP Address.
The sample body is string body = "<p>Please click here</p>Thank You."
The problem is : between IP Address and Port.
Successful send an email if i remove :.
Do you guys have any ideas?
public void Sent(string sender, string receiver, string subject, string body)
{
using (MailMessage mail = new MailMessage(sender, receiver))
{
using (SmtpClient client = new SmtpClient())
{
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "mail.companyName.com.my";
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
client.Send(mail);
}
}
}
You are doing it right, the code to send the mail is ok (you may want to revise the function name and make the smtp host name configurable, but that is not the point here).
The e-mail delivery fails on a relay, there is no immedieate feedback (no exception) to the client about this kind of failure.
The best bet is the IncreaseScoreWithRedirectToOtherPort property set in Set-HostedContentFilterPolicy in case your mail provider is Office365, or a similar spam filter mechanism in any other mail provider that is encountered down the mail delivery chain.
You can set a reply-to address and hope that the destination server will bounce a delivery failure that gives you more information. Or have the admin of the mail server look up the logs. More information here:
https://serverfault.com/questions/659861/office-365-exchange-online-any-way-to-block-false-url-spam
Try setting the 'mail.Body' to receive a Raw Html message instead of a encoded string, like:
mail.Body = new System.Web.Mvc.HtmlHelper(new System.Web.Mvc.ViewContext(), new System.Web.Mvc.ViewPage()).Raw(body).ToString();
Or put a using System.Web.Mvc at the beginning so it gets shorter and easier to understand:
using System.Web.Mvc
mail.Body = new HtmlHelper(new ViewContext(), new ViewPage()).Raw(body).ToString();
I get this error when I try to send an e-mail to a specific address in my code:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: Unknown user
The code sends an e-mail to two email addresses, mine and my colleague's. The e-mail sends to me just fine, but I get that error when it tries to send the email to him.
I looked around, and basically the common explanation for this error is that the email address is invalid, or their mailbox is full and isn't allowed to receive mail, or there is some setting on the server that is restricting it from receiving an e-mail.
But the email address is able to receive email, I'm corresponding back and forth through e-mail with him right now.
Is there any other reason why this error might occur?
EDIT:
Here's the code, maybe someone can spot an issue. I checked the parameters being passed, all the data is correct:
private static void SendEmail(IEnumerable<MailAddress> to, MailAddress from,
string subject, string body, string bodyHtml)
{
var mail = new MailMessage { From = from, Subject = subject };
foreach (var address in to)
{
mail.To.Add(address);
}
mail.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(bodyHtml, null, "text/html"));
mail.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(body, null, "text/plain"));
try
{
var smtp = new SmtpClient("localhost", 25)
{
Credentials = new NetworkCredential("xxx", "xxx")
};
smtp.Send(mail);
}
catch (Exception err)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(err);
}
}
Assuming your SMTP settings are correct this is most probably a case of a server-side restriction...
For example to prevent spam the server only accepts smtp from static sender IP and/or is checking sender IP against MX records (DNS) etc.