我想使用System.Web.Mail.MailMessage和System.Web.Mail.SmtpMail类发送电子邮件。 我知道这是一个弃用的类,但使用System.Net.Mail对我来说不是一个选项。
到目前为止,我能够发送html格式的邮件,但在发送时我无法将图片嵌入到我的电子邮件中。
这是我到目前为止所尝试过的;
private static void SendMailMethod(string mailServer, int mailServerPort, string userName, string password)
{
const string SMTP_SERVER = "http://schemas.microsoft.com/cdo/configuration/smtpserver";
const string SMTP_SERVER_PORT =
"http://schemas.microsoft.com/cdo/configuration/smtpserverport";
const string SEND_USING = "http://schemas.microsoft.com/cdo/configuration/sendusing";
const string SMTP_USE_SSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl";
const string SMTP_AUTHENTICATE =
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate";
const string SEND_USERNAME =
"http://schemas.microsoft.com/cdo/configuration/sendusername";
const string SEND_PASSWORD =
"http://schemas.microsoft.com/cdo/configuration/sendpassword";
var mailMessage = new System.Web.Mail.MailMessage();
mailMessage.Fields[SMTP_SERVER] = mailServer;
mailMessage.Fields[SMTP_SERVER_PORT] = mailServerPort;
mailMessage.Fields[SEND_USING] = 2;
mailMessage.Fields[SMTP_USE_SSL] = false;
mailMessage.Fields[SMTP_AUTHENTICATE] = 0;
mailMessage.Fields[SEND_USERNAME] = userName;
mailMessage.Fields[SEND_PASSWORD] = password;
mailMessage.From = "abc@xyz.com";
mailMessage.To = "abc@xyz.com";
mailMessage.Subject = "Test mail:";
mailMessage.BodyFormat = MailFormat.Html;
var mailAttachment = new MailAttachment("E:\\imageToEmbed.jpg");
mailMessage.Attachments.Add(mailAttachment);
mailMessage.Attachments.Add(new MailAttachment("E:\\TestAttachmentFile.txt"));
var htmlBody =
"<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
"<html xmlns = \"http://www.w3.org/1999/xhtml\" >" +
" <head >" +
"<meta http - equiv = \"content-type\" content = \"text/html; charset=UTF-8\" />" +
"</head >" +
"<body style = \"font-family: Segoe UI; text-align:left;\" >" +
"Following is an embedded image:" +
"<br />" +
"<img alt = \"\" src = \"imageToEmbed.jpg\" />" +
"</body >" +
"</html >";
mailMessage.Body = htmlBody;
try
{
SmtpMail.Send(mailMessage);
Console.WriteLine("Mail sent");
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex.ToString());
throw ex;
}
}
此代码发送电子邮件,但邮件显示的是带有十字形的小方块,而不是显示图像。 如何将此“imageToEmbed.jpg”嵌入到我的电子邮件中。
答案 0 :(得分:2)
使用类System.Web.Mail.Message
无法实现这一点,因为该实现抽象了对基础Collaboration Data Objects component的许多功能的访问。
您最好切换到直接使用CDO COM对象,而不是使用Microsoft CDO for Windows 2000 Library
提供的包装器。在参考对话框的COM选项卡中添加对var cdo = new CDO.Message();
// configuration
var cfg = cdo.Configuration;
cfg.Fields[SMTP_SERVER].Value = "smtp.server.com";
cfg.Fields[SMTP_SERVER_PORT].Value = 22;
cfg.Fields[SEND_USING].Value = 2;
cfg.Fields[SMTP_USE_SSL].Value = true;
cfg.Fields[SMTP_AUTHENTICATE].Value = 1;
cfg.Fields[SEND_USERNAME].Value = "user@example.com";
cfg.Fields[SEND_PASSWORD].Value = "password";
cfg.Fields.Update();
cdo.To = "awesome@example.com";
cdo.Sender = "me@example.com";
// attachment
var cdoatt = cdo.AddAttachment("file:///E:/imageToEmbed.jpg");
//this is why System.Web.Mail can't embed images
cdoatt.Fields["urn:schemas:mailheader:content-id"].Value = Guid.NewGuid().ToString("N");
cdoatt.Fields.Update();
// get a reference to out content-id field
var cid = cdoatt.Fields["urn:schemas:mailheader:content-id"];
// notice the special layout of SRC on the image tag,
//it will be somethong like CID:123456789abcdef
cdo.HTMLBody = @"<HTML><BODY><B>CDO</B><BR /> <IMG SRC=""cid:" + cid.Value + @"""/></BODY></HTML>";
cdo.Send();
的引用。
之后,您可以使用以下代码创建并发送带有嵌入图像的html电子邮件:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0001_01D1AF72.C8AE8C70"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384
This is a multi-part message in MIME format.
------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_0002_01D1AF72.C8AE8C70"
------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
plain cdo
------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<HTML><BODY><B>plain cdo</B> <IMG SRC="cid:42"/></BODY></HTML>
------=_NextPart_001_0002_01D1AF72.C8AE8C70--
------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: image/png;
name="file.png"
Content-Transfer-Encoding: base64
Content-ID: <42>
Content-Disposition: attachment;
filename="file.png"
iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMA
请注意如何在附件上设置Content-ID标头。这是必需的,因此您可以在图像的SRC属性中使用Content-ID。
典型的邮件消息会显示
System.Web.Mail
注意Content-ID(此处为42)。
使用普通MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0000_01D1AF70.59FC25F0"
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384
This is a multi-part message in MIME format.
------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_0001_01D1AF70.59FC25F0"
------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Following is an embedded image:
_____
test afterit
------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml" > <head ><meta http - equiv = "content-type" content = "text/html; charset=UTF-8" /></head ><body style = "font-family: Segoe UI; text-align:left;" ><i>Following is an embedded image:</i><br /><img alt = "" src ="<file:///file.png>"/><hr><b>test afterit</b></body ></html >
------=_NextPart_001_0001_01D1AF70.59FC25F0--
------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: image/png;
name="file.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="file.png"
iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMAAAAI0l
课程时,您的原始电子邮件将如下所示:
Object.freeze()
正如您所看到的,没有添加Content-ID标头,并且.Net实现中没有添加该标头的代码路径。
答案 1 :(得分:0)
它可能无法在指定的路径中找到图像,或者它可能是一个权限事物,因此破坏的图像很重要。您需要先检查它,但如果您使用base64,则会将图像放在字符串中。
尝试将图片更改为base64 link to a free online converter page.