Problem when sending emails via SMTP using C#
I am sending bulk email notifications to my network and I am having a problem with the SMTP I am using.
The problem is for any email-account I use it sends about 30-40 emails and then I get the following error:
...Mailbox unavailable. The server response was: This email is not accepted, contact your email hosting provider.
Any ideas?
Thanks
code-Windows service:
public SMTP()
{
client = new SmtpClient(host);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(_username, _password);
}
public void Send()
{
using (EmailEntities ent = new EmailEntities())
{
var res = ent.wdj_tbl_EmailQueue.Take(50);
if (res != null)
{
foreach (wdj_tbl_EmailQueue r in res)
{
try
{
Int32 id = r.ID;
eml = new MailMessage();
try
{
AddTo((r.emailTo.Replace("\n", "").Trim() ?? ""));
AddCc((r.emailCC.Replace("\n", "").Trim() ?? ""));
AddBcc((r.emailBCC.Replace("\n", "").Trim() ?? ""));
}
catch
{
ent.wdj_tbl_EmailQueue.DeleteObject(r);
continue;
}
eml.From = new MailAddress(r.emailFrom);
eml.IsBodyHtml = (r.emailIsHTML ?? true);
eml.Subject = r.emailSubject;
fixHeaders("To", eml.To);
fixHeaders("Cc", eml.CC);
if (!eml.IsBodyHtml)
{
eml.Body = (r.emailTextBody ?? "");
}
else
{
eml.AlternateViews.Add(AlternateView.CreateAlternateViewFromString((r.emailHTMLBody ?? ""), new ContentType("text/html")));
eml.Body = (r.emailTextBody ?? "");
eml.IsBodyHtml = false;
}
try
{
client.Send(eml);
}
catch (Exception ex)
{
if (!EventLog.SourceExists("WadjaEmailService"))
EventLog.CreateEventSource("EmailService", "EmailLog");
EventLog.WriteEntry("EmailService", "The email with id " + id.ToString() + " (" + eml.To + ") was not sent: " + ex.Message, EventLogEntryType.Error);
continue;
}
}
catch
{
ent.wdj_tbl_EmailQueue.DeleteObject(r);
continue;
}
ent.wdj_tbl_EmailQueue.DeleteObject(r);
}
ent.SaveChanges();
}
}
}
public void AddTo(String email)
{
//network = GetNetworkName();
string[] emailArray = email.Split(',');
if (emailArray.Length > 1)
{
for (int i = 0; i < emailArray.Length; i++)
{
eml.To.Add(new MailAddress(emailArray[i]));
}
}
else
{
eml.To.Add(new MailAddress(email));
}
}
public void AddTo(String email, String displayName)
{
eml.To.Add(new MailAddress(email, displayName));
}
public void AddCc(String email)
{
if (String.IsNullOrEmpty(email))
return;
string[] emailArray = email.Split(',');
if (emailArray.Length > 1)
{
for (int i = 0; i < emailArray.Length; i++)
{
eml.CC.Add(new MailAddress(emailArray[i]));
}
}
else
{
eml.CC.Add(new MailAddress(email));
}
}
public void AddCc(String email, String displayName)
{
if (String.IsNullOrEmpty(email))
return;
eml.CC.Add(new MailAddress(email, displayName));
}
public void AddBcc(String email)
{
if (String.IsNullOrEmpty(email))
return;
string[] emailArray = email.Split(',');
if (emailArray.Length > 1)
{
for (int i = 0; i < emailArray.Length; i++)
{
eml.Bcc.Add(new MailAddress(emailArray[i]));
}
}
else
{
eml.Bcc.Add(new MailAddress(email));
}
}
public void AddBcc(String email, String displayName)
{
if (String.IsNullOrEmpty(email))
return;
eml.Bcc.Add(new MailAddress(email, displayName));
}
private void fixHeaders(String hName, MailAddressCollection mac)
{
String hTmp = "";
foreach (MailAddress ma in mac)
{
if (!String.IsNullOrEmpty(ma.DisplayName))
hTmp += (", " + ma.DisplayName + "<" + ma.Address + ">");
else
hTmp += (", " + ma.Address);
开发者_开发百科}
while (hTmp.StartsWith(","))
hTmp = hTmp.Substring(1).Trim();
if (!String.IsNullOrEmpty(hTmp))
{
eml.Headers.Add(hName, hTmp);
}
}
If your code is able to send 30-40 emails and then fails, I would first look at any logs generated by the app to make sure that the email address(es) that it is failing on are valid and usable from outside of the app.
Then I would look at the sever logs (if possible) to try to isolate why it is rejecting the later mail.
How fast are you sending these messages? Is it possible that you are running into some kind of throttling on the server?
精彩评论