ASP.NET中使用System.Net.Mail发邮件
2012-11-15 16:04:30 来源:WEB开发网核心提示:MemoryStream imgStream = GetImage();imgStream.Position = 0;//非常重要,否则图片为空!在邮件里显示红叉LinkedResource lrImage = new LinkedResource(imgStream, System.Net.Mime.MediaTyp
MemoryStream imgStream = GetImage();
imgStream.Position = 0;//非常重要,否则图片为空!在邮件里显示红叉
LinkedResource lrImage = new LinkedResource(imgStream, System.Net.Mime.MediaTypeNames.Image.Jpeg);
lrImage.ContentId = "imageInEmail"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片
htmlBody.LinkedResources.Add(lrImage);
message.AlternateViews.Add(htmlBody);
MailAddress copy = new MailAddress("mailto:CopyToEmail@website.com");
message.CC.Add(copy);
SmtpClient client = new SmtpClient("SMTP Server");
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
}
public static MemoryStream GetImage()
{
//Code here... ...
}
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net.Mail; using System.Text; using System.IO; using System.Net; using System.Net.Mime; namespace BI.MailService.Mail { public class BIMailService { SmtpClient SmtpClient = null; //设置SMTP协议 MailAddress MailAddress_from = null; //设置发信人地址 当然还需要密码 MailAddress MailAddress_to = null; //设置收信人地址 不需要密码 MailMessage MailMessage_Mai = null; FileStream FileStream_my = null; //附件文件流 #region 设置Smtp服务器信息 /// <summary> /// 设置Smtp服务器信息 /// </summary> /// <param name="ServerName">SMTP服务名</param> /// <param name="Port">端口号</param> private void setSmtpClient(string ServerHost, int Port) { SmtpClient = new SmtpClient(); SmtpClient.Host = ServerHost;//指定SMTP服务名 例如QQ邮箱为 smtp.qq.com 新浪cn邮箱为 smtp.sina.cn等 SmtpClient.Port = Port; //指定端口号 SmtpClient.Timeout = 0; //超时时间 } #endregion #region 验证发件人信息 /// <summary> /// 验证发件人信息 /// </summary> /// <param name="MailAddress">发件邮箱地址</param> /// <param name="MailPwd">邮箱密码</param> private void setAddressform(string MailAddress, string MailPwd) { //创建服务器认证 NetworkCredential NetworkCredential_my = new NetworkCredential(MailAddress, MailPwd); //实例化发件人地址 MailAddress_from = new System.Net.Mail.MailAddress(MailAddress, "报表系统"); MailMessage_Mai.From = MailAddress_from; //指定发件人信息 包括邮箱地址和邮箱密码 SmtpClient.Credentials = new System.Net.NetworkCredential(MailAddress_from.Address, MailPwd); } #endregion #region 检测附件大小 private bool Attachment_MaiInit(string path) { try { FileStream_my = new FileStream(path, FileMode.Open); string name = FileStream_my.Name; int size = (int)(FileStream_my.Length / 1024 / 1024); FileStream_my.Close(); //控制文件大小不大于10M if (size > 10) { return false; } return true; } catch (IOException e) { return false; } } #endregion #region 添加收件人 /// <summary> /// 添加收件人列表 /// </summary> /// <param name="list">收件人名单</param> private void AddAddressee(string[] list) { foreach (string address in list) { if (address != list[0])//第一个收件人已经添加 { MailAddress_to = new MailAddress(address); MailMessage_Mai.To.Add(MailAddress_to); } //MailMessage_Mai.CC.Add(new MailAddress("test@beisen.com"));//将收件人加入域中 //MailMessage_Mai.Bcc.Add(new MailAddress("1234567@qq.com")); } } #endregion public string SendMessage(string server, string accessoriesPath, string[] toMailAddressList, string subject, string body) { try { MailMessage_Mai = new MailMessage("test@gmail.com", toMailAddressList[0]); MailMessage_Mai.Subject = subject; MailMessage_Mai.SubjectEncoding = Encoding.UTF8;//进行字符格式转换,否则可能收到的邮件出现乱码 MailMessage_Mai.Body = body; MailMessage_Mai.BodyEncoding = Encoding.UTF8; MailMessage_Mai.IsBodyHtml = true; MailMessage_Mai.Priority = MailPriority.High;//指定邮件优先级 SmtpClient = new SmtpClient(server); string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容"; MailMessage_Mai.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain")); string htmlBodyContent = "如果你的看到<b>这个</b>,说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>"; htmlBodyContent += "<a href=\"http://www.baidu.com//\">百度一下</a><img src=\"cid:imageInEmail\"/>"; AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html"); MemoryStream imgStream = GetImage(); imgStream.Position = 0;//非常重要,否则图片为空!在邮件里显示红叉 LinkedResource lrImage = new LinkedResource(imgStream, System.Net.Mime.MediaTypeNames.Image.Jpeg); lrImage.ContentId = "imageInEmail"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片 htmlBody.LinkedResources.Add(lrImage); MailMessage_Mai.AlternateViews.Add(htmlBody); setSmtpClient("smtp.gmail.com", 587);//587这是gmail的端口号,一般中国的邮箱发不了邮件就是因为端口号封闭了,不能提供第三方开发者调用。 setAddressform("test@gmail.com", "mima"); if (toMailAddressList.Length > 1) { AddAddressee(toMailAddressList); } SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpClient.EnableSsl = true; if (accessoriesPath.Trim() != "") { MailMessage_Mai.Attachments.Clear();//添加附件 MailMessage_Mai.HeadersEncoding = Encoding.UTF8; MailMessage_Mai.Attachments.Add(new Attachment(accessoriesPath, MediaTypeNames.Application.Octet)); } SmtpClient.Send(MailMessage_Mai); return "发送成功!"; } catch (Exception ey) { throw ey; } } public static MemoryStream GetImage() { //Code here... ... return null; } } }
- ››ASP.NET及JS中的cookie基本用法
- ››使用linux中的quota教程
- ››ASP.NET获取MS SQL Server安装实例
- ››使用jxl生成带动态折线图的excel
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››使用mysql mysqldump进行数据库迁移
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››使用jquery是新tab形式
更多精彩
赞助商链接