使用C#通过SMTP发送电子邮件时出现问题

时间:2011-04-05 05:13:57

标签: windows web-services c#-4.0 smtp

我正在向我的网络发送批量电子邮件通知,而我正在使用的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);

        }
    }

1 个答案:

答案 0 :(得分:1)

如果您的代码能够发送30-40封电子邮件然后失败,我会首先查看该应用生成的任何日志,以确保其失败的电子邮件地址有效且可从外部使用该应用程序。

然后我会查看服务器日志(如果可能的话),试图找出拒绝后来邮件的原因。

您发送这些邮件的速度有多快?您是否有可能在服务器上遇到某种限制?