NET发HTML格式的邮件,把图片等引用文件作为附件资源 代码如下: foreach (string file in fileList) { //把图片名称全换成小写 string fileNameExt = Path.GetFileName(file).ToLower(); string fileName = Path.GetFileNameWithoutExtension(fileNameExt); string fileExt = Path.GetExtension(fileNameExt).TrimStart(new char[] { '.' }); //判断是否是图片 if (fileExt == "gif" || fileExt == "jpg" || fileExt == "jpeg" || fileExt == "bmp" || fileExt == "png") { //把原文件中的图片链接地址全部换成cid+图片ID的模式 byte[] bytearray = utf8.GetBytes(fileName); //使用UTF8生成的新图片ID foreach (byte byinfo in bytearray) { newimageId.Append(byinfo); } //构造邮件资源文件列表。 if (File.Exists(file)) { LinkedResource logo = new LinkedResource(file, "image/jpeg"); //配置图片ID 一定要和邮件内容中的cid后的值一样 logo.ContentId = newimageId.ToString(); lRes.Add(logo); } body = body.Replace(EmailManager.FILE_SERVER_IMG_FOLDER + "/" + fileNameExt, "cid:" + newimageId.ToString()); newimageId.Remove(0, newimageId.Length); } else if (fileExt == "css") { //读取CSS文件插入到页面的HTML代码中 string strcss = "<mce:style type=text/css><!-- " + File.ReadAllText(file) + " --></mce:style><style type=text/css mce_bogus="1">" + File.ReadAllText(file) + "</style>"; int splitIndex; string tmpBody = this.InsertTextIntoContent(body, strcss, "</title>", out splitIndex); if (splitIndex == -1) { tmpBody = this.InsertTextIntoContent(body, strcss, "</style>", out splitIndex); if (splitIndex == -1) { tmpBody = strcss + tmpBody; } } body = tmpBody; } } AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); //追加链接资源文件。 foreach (LinkedResource lr in lRes) { htmlView.LinkedResources.Add(lr); } //将配置好的信息添加到邮件信息中 mail.AlternateViews.Add(htmlView); SmtpClient smtpclient = new SmtpClient(account.SMTPServer, account.Port); //验证信息 smtpclient.Credentials = new NetworkCredential(account.Account, account.Password); //发送邮件 smtpclient.Send(mail);