完整程序:
有时候需要系统发一些带图片的邮件给用户,一般的做法是将邮件中的图版放到公网上,然后在HTML邮件中指定绝对地址.但这样做有一些bug,就是outlook2003等其他一些客户端会禁止你看这些图片.
现在最好的做法是将图版放在邮件中,发送一个带图的HTML邮件给客户.
下面是 .NET 发邮件中的关键代码.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace cutEmpAccount
{
class EmailSimple
{
private static string _content = @"<p>系统维护人员:<br />
<img src='cid:[attid]' />
取消帐号出错,请跟进。</p>";
public static void sentEmail(string empId, string name, string deptCode, string deptName)
{
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = false;
smtp.Host = "smtp.extjs.org.cn";
smtp.Port = 25;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential("fatjames", "fatjames");
MailMessage em = new MailMessage();
em.Subject = "停帐号出错通知";
em.SubjectEncoding = Encoding.GetEncoding(936);
em.From = new MailAddress("fatjames@extjs.org.cn", "停帐号系统",Encoding.GetEncoding(936));
em.To.Add(System.Configuration.ConfigurationSettings.AppSettings["ErrEmail"]);
em.IsBodyHtml = true;
em.BodyEncoding = Encoding.GetEncoding(936);
string fileName = System.IO.Directory.GetCurrentDirectory().ToString() + "//PA.JPG";
LinkedResource lrImage = new LinkedResource(fileName, "image/jpeg");
icontent = icontent.Replace("[attid]", lrImage.ContentId);
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(icontent, null, "text/html");
htmlBody.LinkedResources.Add(lrImage);
em.AlternateViews.Add(htmlBody);
smtp.Send(em);
}
}
} |