最近一个项目需求要求可以发送email,刚开始调用了SPUtility.SendEmail方法:
SPUtility.SendEmail(pSPWeb, true, true, pEmailAddress, pSubject, pBody);
开发环境测试的时候发现Html 标签无法在邮件中转换格式,全部都显示为文本。再后来当邮件Body长度超过2048个字符后,将会被自动截掉。google了没发现好的解决办法,后来又转会到用 System.Net.Mail 发送邮件,主要代码:
Code
1 private void SendEmail(string pSubject, string pFrom, string pTo, string pBody, string pPassword)
2 {
3 SmtpClient client = new SmtpClient();
4 client.Host = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
5 client.Port = 25;
6 client.DeliveryMethod = SmtpDeliveryMethod.Network;
7 client.UseDefaultCredentials = false;
8 client.Credentials = new System.Net.NetworkCredential(pFrom, pPassword);
9
10 MailMessage mail = new MailMessage(pFrom, pTo, pSubject, pBody);
11 mail.IsBodyHtml = true;
12 mail.ReplyTo = new MailAddress(SPContext.Current.Site.WebApplication.OutboundMailReplyToAddress);
13
14 client.Send(mail);
15 }
除此之外,SPUtility.SendEmail好像无法添加附件,算是一个缺陷。