需要使用后台工作进程发送电子邮件

时间:2011-03-09 06:47:05

标签: c# winforms multithreading smtp backgroundworker

我编写了用于在C#中发送电子邮件的代码,但是当应用程序发送的邮件大小超过2 MB的附件时,应用程序会挂起。我被建议SO用户使用后台工作程序进程。

我已经通过MSDN的后台工作程序进程示例,并且谷歌也是,但我不知道如何集成到我的代码中。

请指导我...

感谢

更新:已添加电子邮件代码

public static void SendMail(string fromAddress, string[] toAddress, string[] ccAddress, string[] bccAddress, string subject, string messageBody, bool isBodyHtml, ArrayList attachments, string host, string username, string pwd, string port)
{
  Int32 TimeoutValue = 0;
  Int32 FileAttachmentLength = 0;
  {
    try
    {
      if (isBodyHtml && !htmlTaxExpression.IsMatch(messageBody))
        isBodyHtml = false;
      // Create the mail message
      MailMessage objMailMsg;
      objMailMsg = new MailMessage();
      if (toAddress != null) {
        foreach (string toAddr in toAddress)
          objMailMsg.To.Add(new MailAddress(toAddr));
      }
      if (ccAddress != null) {
        foreach (string ccAddr in ccAddress)
          objMailMsg.CC.Add(new MailAddress(ccAddr));
      }
      if (bccAddress != null) {
        foreach (string bccAddr in bccAddress)
          objMailMsg.Bcc.Add(new MailAddress(bccAddr));
      }
      if (fromAddress != null && fromAddress.Trim().Length > 0) {
        //if (fromAddress != null && fromName.trim().length > 0)
        //    objMailMsg.From = new MailAddress(fromAddress, fromName);
        //else
        objMailMsg.From = new MailAddress(fromAddress);
      }
      objMailMsg.BodyEncoding = Encoding.UTF8;
      objMailMsg.Subject = subject;
      objMailMsg.Body = messageBody;
      objMailMsg.IsBodyHtml = isBodyHtml;
      if (attachments != null) {
        foreach (string fileName in attachments) {
          if (fileName.Trim().Length > 0 && File.Exists(fileName)) {
             Attachment objAttachment = new Attachment(fileName);
             FileAttachmentLength=Convert.ToInt32(objAttachment.ContentStream.Length);
             if (FileAttachmentLength >= 2097152) {
               TimeoutValue = 900000;
             } else {
                TimeoutValue = 300000;
             }
             objMailMsg.Attachments.Add(objAttachment);
             //objMailMsg.Attachments.Add(new Attachment(fileName)); 
           }
        }
      }
      //prepare to send mail via SMTP transport
      SmtpClient objSMTPClient = new SmtpClient();
      if (objSMTPClient.Credentials != null) { } else {
        objSMTPClient.UseDefaultCredentials = false;
        NetworkCredential SMTPUserInfo = new NetworkCredential(username, pwd);
        objSMTPClient.Host = host;
        objSMTPClient.Port = Int16.Parse(port);
        //objSMTPClient.UseDefaultCredentials = false;
        objSMTPClient.Credentials = SMTPUserInfo;
        //objSMTPClient.EnableSsl = true;
        //objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;
      }
      //objSMTPClient.Host = stmpservername;
      //objSMTPClient.Credentials
      //System.Net.Configuration.MailSettingsSectionGroup mMailsettings = null;
      //string mailHost = mMailsettings.Smtp.Network.Host;
      try {
        objSMTPClient.Timeout = TimeoutValue;
        objSMTPClient.Send(objMailMsg);
        //objSMTPClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
        objMailMsg.Dispose();
      }
      catch (SmtpException smtpEx) {
        if (smtpEx.Message.Contains("secure connection")) {
           objSMTPClient.EnableSsl = true;
           objSMTPClient.Send(objMailMsg);
        }
      }
    }
    catch (Exception ex)
    {
       AppError objError = new AppError(AppErrorType.ERR_SENDING_MAIL, null, null, new AppSession(), ex);
       objError.PostError();
       throw ex;
    }
  }
}

我无法修改此处的代码,因为无论何时从我的应用程序发送邮件,都会调用常用方法。

2 个答案:

答案 0 :(得分:6)

您可以启动后台线程以继续循环并发送电子邮件:

private void buttonStart_Click(object sender, EventArgs e)
{
    BackgroundWorker bw = new BackgroundWorker();
    this.Controls.Add(bw);
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.RunWorkerAsync();
}

private bool quit = false;
void bw_DoWork(object sender, DoWorkEventArgs e)
{
    while (!quit)
    {
        // Code to send email here
    }
}

替代方法:

private void buttonStart_Click(object sender, EventArgs e)
{
    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
    client.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(client_SendCompleted);
    client.SendAsync("from@here.com", "to@there.com", "subject", "body", null);
}

void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error == null)
        MessageBox.Show("Successful");
    else
        MessageBox.Show("Error: " + e.Error.ToString());
}

特定于您的示例,您应该替换以下内容:

try
{
    objSMTPClient.Timeout = TimeoutValue;
    objSMTPClient.Send(objMailMsg);
    //objSMTPClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
    objMailMsg.Dispose();
}
catch (SmtpException smtpEx)
{
    if (smtpEx.Message.Contains("secure connection"))
    {
        objSMTPClient.EnableSsl = true;
        objSMTPClient.Send(objMailMsg);
    }
}

以下内容:

objSMTPClient.Timeout = TimeoutValue;
objSMTPClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
objSMTPClient.SendAsync(objMailMsg, objSMTPClient);

进一步向下,包括:

void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error == null)
        MessageBox.Show("Successful");
    else if (e.Error is SmtpException)
    {
        if ((e.Error as SmtpException).Message.Contains("secure connection"))
        {
            (e.UserState as SmtpClient).EnableSsl = true;
            (e.UserState as SmtpClient).SendAsync(objMailMsg, e.UserState);
        }
        else
            MessageBox.Show("Error: " + e.Error.ToString());
    }
    else
        MessageBox.Show("Error: " + e.Error.ToString());
}

答案 1 :(得分:1)

有一个很好的例子,说明如何使用Richard Kiessig的书“Ultra-Fast ASP.NET”第8章中的“Service Broker”来做到这一点。

以下是发布商网站上该书的链接,您可以从该书中下载示例代码。再次,第8章......

http://apress.com/book/view/9781430223832