您现在的位置是:网站首页> 编程资料编程资料
.net中 发送邮件内容嵌入图片的具体实例_实用技巧_
2023-05-24
348人已围观
简介 .net中 发送邮件内容嵌入图片的具体实例_实用技巧_
例程一
邮件内容调用图片格式为:
发送邮件的服务端代码为:
SmtpClient 发送邮件的对象 //代码省略
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From="发送者邮箱";
mailMessage.To.Add("收件人邮件列表");
mailMessage.CC.Add("抄送人邮件列表");
mailMessage.Subject = subject;
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(content,null,"text/html");
LinkedResource lrImage = new LinkedResource("a.jpg","image/gif");
lrImage.ContentId = "Email001";
htmlBody.LinkedResources.Add(lrImage);
mailMessage.AlternateViews.Add(htmlBody);
SmtpClient.Send(mailMessage);
例程二
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.163.com";
smtp.Credentials = new NetworkCredential("renzhijie1111", "**");
MailMessage mm = new MailMessage();
mm.From = new MailAddress("renzhijie1111@163.com", "无敌任志杰测试");
mm.To.Add("renzhijie1990@vip.qq.com");
mm.Subject = "发送带图片邮件";
string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
mm.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));
////HTML格式邮件的内容
string htmlBodyContent = "如果你的看到这个, 说明你是在以 HTML 格式查看邮件
";
htmlBodyContent += ""; //注意此处嵌入的图片资源
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
LinkedResource lrImage = new LinkedResource(@"d:\1.jpg", "image/gif");
lrImage.ContentId = "weblogo"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片
htmlBody.LinkedResources.Add(lrImage);
mm.AlternateViews.Add(htmlBody);
////要求回执的标志
mm.Headers.Add("Disposition-Notification-To", "renzhijie1111@163.com");
////自定义邮件头
mm.Headers.Add("X-Website", "https://www.jb51.net/");
////针对 LOTUS DOMINO SERVER,插入回执头
mm.Headers.Add("ReturnReceipt", "1");
mm.Priority = MailPriority.Normal; //优先级
mm.ReplyTo = new MailAddress("renzhijie1111@163.com", "我自己");
////如果发送失败,SMTP 服务器将发送 失败邮件告诉我
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
////异步发送完成时的处理事件
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
////开始异步发送
smtp.SendAsync(mm, null);
相关内容
- asp.net无法获取iis目录的问题解决方法_实用技巧_
- asp.net网站首页根据IP自动跳转指定页面的示例_实用技巧_
- asp.net querystring乱码解决方法_实用技巧_
- asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码_实用技巧_
- asp.net操作ini文件示例_实用技巧_
- 使用ajax局部刷新gridview进行数据绑定示例_实用技巧_
- asp.net获取网站绝对路径示例_实用技巧_
- viewstate和datatable动态录入数据示例_实用技巧_
- 将文本文件的内容或者文字保存成图片的方法分享_实用技巧_
- gridview实现服务器端和客户端全选的两种方法分享_实用技巧_
