public bool SendMail(string fromAdd, string[] toAdd, string subject, string body, string[] fileName)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(fromAdd);
for (int i = 0; i < toAdd.Length; i++)
{
if (toAdd[i].Trim() != string.Empty)
{
mailMessage.To.Add(new MailAddress(toAdd[i]));
}
}
mailMessage.Priority = MailPriority.High;
mailMessage.IsBodyHtml = true;
mailMessage.Subject = subject;
mailMessage.Body = body;
if (fileName != null)
{
for (int i = 0; i < fileName.Length; i++)
{
string filePath = HttpContext.Current.Server.MapPath("~/wpresources/OAContactsManage/upload/") + fileName[i].ToString();
mailMessage.Attachments.Add(new Attachment(filePath));
}
}
try
{
//通过对象模型取得moss配置的smtp服务器
SPOutboundMailServiceInstance smtpServer = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer.Server.Address);
smtp.Send(mailMessage);
}
catch (Exception ex)
{
return false;
}
finally
{
foreach (Attachment item in mailMessage.Attachments)
{
item.Dispose(); //一定要释放该对象,否则无法删除附件
}
DeleteFile(fileName); //不论邮件是否发送成功,删除服务器上临时附件
}
return true;
}