首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在asp.net中使用aync发送多封邮件

在asp.net中使用aync发送多封邮件
EN

Stack Overflow用户
提问于 2014-12-29 13:24:28
回答 1查看 421关注 0票数 1

我必须向多个收件人发送邮件。一个组织的所有员工。为此,我通过搜索引擎浏览了资源,并决定制作多个即时SmtpClient,并使用异步发送邮件。因此,对于测试,我编写了以下代码,其中包含gmail测试服务器。

代码语言:javascript
复制
public void SendMail()
{
    try
    {
        string strEmail = string.Empty;
        // for collecting multiple recepient
        //foreach (GridViewRow grd in gdv_txtMailTo.Rows)
        //{
        //    CheckBox chkBx = (CheckBox)grd.FindControl("chkBxSelect");
        //    if (chkBx != null && chkBx.Checked)
        //    {
        //        strEmail += ((Label)grd.FindControl("Label1")).Text + ',';
        //    }

        //}
        strEmail = "yogendra.paudyal44@gmail.com";
        string emails = strEmail;
        string output = DBNull.Value.ToString();
        //if (emails != "")
        //{
        //    output = emails.Remove(emails.Length - 1, 1);
        //}
        MassMail_Controller.SaveSelectedEmails(output, PortalID);
        MassMail_Info info = new MassMail_Info();
        info.SendFrom = txtMailFrom.Text;
        info.Subject = txtSubject.Text;
        info.CC = txtCC.Text;
        info.BCC = txtBCC.Text;
        info.FileName = "";
        info.SendTo = strEmail;
        string messageTemplate = txtBody.Text;
        info.Body = messageTemplate;
        info.UserModuleId = UserModuleID;
        info.PortalId = PortalID;
        for (int i = 0; i < 50; i++) {
            MailHelper.SendMailOneAttachment(info.SendFrom, info.SendTo, info.Subject, info.Body, info.FileName, info.CC, info.BCC);
        }

        //Thread thread = new Thread(new ParameterizedThreadStart(GetAllEmail));

        //thread.IsBackground = true;
        //thread.Start(bit);

        //while (thread.IsAlive)
        //{
        //    ShowMessage(SageMessageTitle.Exception.ToString(), "Mail Sent", "", SageMessageType.Success);


        //}
    }
    catch (Exception ex)
    {
        ProcessException(ex);
    }


} 

而mailHelper将是:

代码语言:javascript
复制
public static void SendEMail(string From, string sendTo, string Subject, string Body, ArrayList AttachmentFiles, string CC, string BCC, bool IsHtmlFormat)
        {
            SageFrameConfig sfConfig = new SageFrameConfig();
            //string ServerPort = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPServer);
            //string SMTPAuthentication = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPAuthentication);
            //string SMTPEnableSSL = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPEnableSSL);
            //string SMTPPassword = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPPassword);
            //string SMTPUsername = sfConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.SMTPUsername);
            string ServerPort = (SageFrameSettingKeys.SMTPServer);
            string SMTPAuthentication =(SageFrameSettingKeys.SMTPAuthentication);
            string SMTPEnableSSL = (SageFrameSettingKeys.SMTPEnableSSL);
            string SMTPPassword = (SageFrameSettingKeys.SMTPPassword);
            string SMTPUsername = (SageFrameSettingKeys.SMTPUsername);
            string[] SMTPServer = ServerPort.Split(':');
            try
            {
                MailMessage myMessage = new MailMessage();
                myMessage.To.Add(sendTo);
                myMessage.From = new MailAddress(From);
                myMessage.Subject = Subject;
                myMessage.Body = Body;
                myMessage.IsBodyHtml = true;

                if (CC.Length != 0)
                    myMessage.CC.Add(CC);

                if (BCC.Length != 0)
                    myMessage.Bcc.Add(BCC);

                if (AttachmentFiles != null)
                {
                    foreach (string x in AttachmentFiles)
                    {
                        if (File.Exists(x)) myMessage.Attachments.Add(new Attachment(x));
                    }
                }
                SmtpClient smtp = new SmtpClient();
                if (SMTPAuthentication == "1")
                {
                    if (SMTPUsername.Length > 0 && SMTPPassword.Length > 0)
                    {
                        smtp.Credentials = new System.Net.NetworkCredential(SMTPUsername, SMTPPassword);
                    }
                }
                smtp.EnableSsl = bool.Parse(SMTPEnableSSL.ToString());
                if (SMTPServer.Length > 0)
                {
                    if (SMTPServer[0].Length != 0)
                    {
                        smtp.Host = SMTPServer[0];
                        if (SMTPServer.Length == 2)
                        {
                            smtp.Port = int.Parse(SMTPServer[1]);
                        }
                        else
                        {
                            smtp.Port = 25;
                        }
                        object userState = myMessage;

                        //wire up the event for when the Async send is completed
                        smtp.SendCompleted += new
                        SendCompletedEventHandler(SendCompletedCallback);

                        smtp.SendAsync(myMessage,userState);
                        Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
                        //string answer = Console.ReadLine();
                        // If the user canceled the send, and mail hasn't been sent yet, 
                        // then cancel the pending operation. 
                        //if (answer.StartsWith("c"))
                        //{
                        //    smtp.SendAsyncCancel();
                        //}
                        //// Clean up.
                        //myMessage.Dispose();
                        Console.WriteLine("Goodbye.");
                    }
                    else
                    {
                        throw new Exception("SMTP Host must be provided");
                    }
                }

            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

这段代码运行良好,但我无法发送指定数量的邮件。发送的电子邮件数量是不同的,每次我执行这段代码。可能是35,40,42等等。似乎SmtpClient的一些实例失败了,但我没有得到任何异常。我是不是做错了什么。我们是否有更好的选择一次发送多封邮件?

EN

回答 1

Stack Overflow用户

发布于 2014-12-29 13:41:31

我想分享我发送电子邮件的经验。我也曾经发送过B-Day电子邮件,但在我的情况下,通常有100-150人,所有邮件都能成功送达。我为此制作了一个web服务,它唯一的任务就是发送电子邮件。但在电子邮件开始成功交付之前,我们在本地机器上进行了测试,它工作得很好,但当我们在服务器上测试它时,我面临着同样的问题,导致这个失败的原因是我们在.Net框架2.0中部署了我们的web服务,然后我们将其更改为4.0,并再次测试了它与10000,500,000,1000封电子邮件,它工作得很好,不是所有的电子邮件都工作得很好,但大多数邮件都到达了destination.Also。另外值得一提的是,我们发送电子邮件的地址被网络部门限制在电子邮件服务器上,只能发送100 emails.Also,尽量避免从一个发件人和一个电子邮件服务器发送太多的电子邮件,因为你可能会被列入黑名单。

摘要

  • 首先检查您是否正在使用.Net framework 2.0如果是,请切换到4.0。
  • 确保网络部门对电子邮件服务器以及您用作发件人的地址没有限制。
  • 将您的所有代码放在using语句中,以确保对象被释放。
  • 按块发送电子邮件(一次大约100封)。

< code >F29

using()

{

//将您发送代码的电子邮件放在此处。

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27683801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档