我正在向我的網絡發送批量電子郵件通知,並且我遇到了使用SMTP的問題。使用C#通過SMTP發送電子郵件時出現問題
問題對於任何電子郵件帳戶,我用它發送約30-40電子郵件,然後我得到以下錯誤:
...郵箱無法使用。服務器的迴應是:這封電子郵件不被接受,請聯繫您的電子郵件主機提供商。
任何想法?
感謝
代號爲Windows服務:
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);
}
}
你使用System.Net.Mail.SmtpClient類嗎?你能發送你的代碼嗎? – 2011-04-05 05:16:32
是的,我已經發布了上面的代碼片段。 – RRG 2011-04-05 14:29:20