asp.net发送邮件并在正文中插入图片的代码分享
本文导语: asp.net在邮件正文中插入图片,代码如下: 代码示例: #region 发送邮件 /// /// 通过附件发送邮件 /// /// 发送人邮箱地址 /// 图片地址 /// 发送人姓名 /// 收件人集合 /// 抄送人集合 /// 附件路径 /// 主题 /// 正文内容 /// ...
asp.net在邮件正文中插入图片,代码如下:
#region 发送邮件
///
/// 通过附件发送邮件
///
/// 发送人邮箱地址
/// 图片地址
/// 发送人姓名
/// 收件人集合
/// 抄送人集合
/// 附件路径
/// 主题
/// 正文内容
///
public static bool SendEmail(string emailFrom, string[] files, string emailFromName, IList zsrList, IList csrList, ArrayList arrFilePath, string mailSubject, string mailBody)
{
MailMessage Email = new MailMessage();
try
{
MailAddress EmailFrom = new MailAddress(emailFrom, emailFromName); //创建发件人邮箱地址对象
Email.From = EmailFrom; //指定发件人
Email.CC.Add(EmailFrom); //将发件人作为抄送人发一份
for (int i = 0; i < zsrList.Count; i++) //循环添加主送人邮箱地址对象
{
MailAddress zsrAddr = new MailAddress(zsrList[i].EMAIL, zsrList[i].NAME);
Email.To.Add(zsrAddr);
}
for (int i = 0; i < csrList.Count; i++) //循环添加抄送人邮箱地址对象
{
MailAddress csrAddr = new MailAddress(csrList[i].EMAIL, csrList[i].NAME);
Email.CC.Add(csrAddr);
}
}
catch
{
return false;
}
Email.IsBodyHtml = true;
Email.BodyEncoding = System.Text.Encoding.UTF8;
Email.Body += mailBody;
for (int i = 0; i < files.Length; i++)
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(files[i]);
Email.Attachments.Add(attachment);
Email.Body += "";
} //by www.
Email.Priority = MailPriority.High;
//邮件主题
Email.Subject = mailSubject;
Email.SubjectEncoding = Encoding.GetEncoding(936);
//邮件附件
for (int i = 0; i < arrFilePath.Count; i++)
{
string file = arrFilePath[i].ToString(); //附件路径
Attachment data = new Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
Email.Attachments.Add(data);
}
SmtpClient Client = new SmtpClient("172.30.1.13");
Client.UseDefaultCredentials = false;
Client.Credentials = new System.Net.NetworkCredential("shoa", "shnetweb.1234");
Client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
Client.Send(Email);
}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable)
{
//Response.Write("Delivery failed - retrying in 5 seconds.");
System.Threading.Thread.Sleep(5000);
Client.Send(Email);
}
}
}
catch (Exception ex)
{
return false;
}
finally
{
for (int i = 0; i < Email.Attachments.Count; i++) //释放占用excel资源
{
Email.Attachments[i].Dispose();
}
}
return true;
}
#endregion