目前,我有一个控制器方法,其中包括一个 try/catch 语句,用于发送循环通过数据库列表的电子邮件:
try
{
// MailSender object declaration
MailSender objMail = new MailSender();
// Set SMTP server address
objMail.Host = "smtp.test.com";
// Sender's address
objMail.From = "test@test.com";
// Subject
objMail.Subject = surveyprogrammodel.Subject;
// HTML format?
objMail.IsHTML = true;
//Send to Mail Queue
objMail.Queue = true;
objMail.QueuePath = Server.MapPath("~\\MailQueue\\");
foreach (var item in EmailList)
{
// Create dynamic link to the take the Survey
var SurveyLink = strUrl + UrlContent + "SurveyResponse/Create?MemberId=" + item.PersonModels.MemberId + "&ProgramId=" + item.ProgramId;
// Recipient's address
objMail.AddAddress(item.PersonModels.Email);
// Body
objMail.Body = surveyprogrammodel.Body + "<br /><br />Please take the survey using the link below:<br /><br /><a href=\"" + SurveyLink + "\">" + SurveyLink + "<a/>";
// Send message
objMail.Send();
objMail.ResetAddresses();
}
}
catch (Exception ex)
{
Response.Write("An error occurred: <font color=red>"+ ex.Message + "</font>");
}
我想知道如何将这个语句分解为一种服务,以便我可以在需要时轻松交换邮件实现。有没有一个很好的例子可以解释实现这一目标的最佳方法?