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

发布于 2024-10-30 10:19:32 字数 5431 浏览 1 评论 0原文

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

        }
    }

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);

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮是我掰弯的 2024-11-06 10:19:32

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

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

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文