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

3
我有一段用C#编写的发送电子邮件的代码,但是当附件大小超过2 MB时,应用程序会挂起。 SO用户建议我使用后台工作进程来解决这个问题。
我已经阅读了MSDN和Google上的后台工作进程示例,但不知道如何将其集成到我的代码中。
请指导我如何操作...
谢谢!
更新:添加了电子邮件代码
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;
    }
  }
}

我无法在这里修改代码,因为这是一个通用方法,每当我的应用程序发送邮件时都会调用它。


如果您能提供当前的代码,那将非常有帮助。我们可以将其转换为后台,而不是提供一个您可能不知道如何适应您的代码示例。 - Fun Mun Pieng
@Fun Mun Pieng:我已经添加了代码... - xorpower
2个回答

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

谢谢您的输入。所以这段代码不需要包含任何后台进程吗? 另外,我在智能感知中没有找到AsyncCompletedEventArgs。可能是什么原因? - xorpower
添加 using System.ComponentModel; 或将其更改为 System.ComponentModel.AsyncCompletedEventArgs - Fun Mun Pieng
谢谢您再次提供输入。上一条评论有效!但是邮件没有被发送出去,不知道为什么... - xorpower
@Xor power,我不确定你是否应该处理你的消息。我还没有检查SendAsync是否实际上复制了消息或使用了你的副本。如果它使用了你的副本并且你调用了dispose,我想在一段时间后会抛出异常并且邮件将失败。如果它复制了一份,那么就没有问题。请尝试一下看看会发生什么。 - Fun Mun Pieng

1

在Richard Kiessig的书“Ultra-Fast ASP.NET”的第8章中,有一个很好的使用“服务代理”来完成这个任务的例子。

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

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


使用 Service Broker 发送电子邮件的优点在于请求被持久化,因此它们可以在应用程序池重启后继续存在,并且如果需要,任务可以移动到单独的服务器以满足安全性和/或可扩展性要求。 - RickNZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接