我正在编写一个在整个应用程序生命周期内发送大量电子邮件的应用程序。
用户提示该应用程序真的没有响应,而且通常很慢。我唯一能想到的原因是大量的电子邮件发送。所以我想我可以通过在不同的线程中发送电子邮件来解决问题!
我在 SPUtility 帮助程序类中使用 SendEmail 方法。现在,我有一个看起来像这样的方法:
public static void SendEmail(SPWeb web, string to, string subject, string body)
{
//Some logic goes here
System.Threading.ThreadPool.QueueUserWorkItem(o =>
{
SPUtility.SendEmail(web, false, false, to, subject, body, false);
});
}
这个方法是在一个 DLL 中定义的,它被许多不同的 webparts 调用。
但这行不通。如果我删除 QueueUserWorkItem 位,让它在线程外发送电子邮件,它就像一个魅力,但话又说回来,它很慢。
我该如何解决这个问题?
最佳答案
我怀疑你最好使用 MailMessage Class (System.Net.Mail)
MailMessage mess =
new MailMessage(
SPContext.Current.Site.WebApplication.OutboundMailReplyToAddress,
sendTo,
subject,
message);
mess.IsBodyHtml = true;
SmtpClient smtp =
new SmtpClient(
SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address);
smtp.Send(mess);
这篇 MSDN 帖子与您的情况相似,Sending Email using SPUtility.SendEmail slow in production
关于c# - SendEmail 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6196114/