我试图在HTML邮件中添加图片。当我将正文保存为 html文件时,它会显示,但是当我通过 GMail发送 html正文作为电子邮件时 strong>,图像不显示。谁能告诉我原因?
我以这种方式设置图像的来源:
var image = body.GetElementsByTagName("img");
string imageAttachmentPath = Path.Combine(Globals.NotificationTemplatesPath, "Header.png");
foreach (XmlElement img in image)
{
img.SetAttribute("src", imageAttachmentPath);
break;
}
//thats the method in which i am sending email.
public static void SendMessageViaEmailService(string from,
string sendTo,
string carbonCopy,
string blindCarbonCopy,
string subject,
string body,
bool isBodyHtml,
string imageAttachmentPath,
Hashtable images,
List<string> attachment,
string title = null,
string embeddedImages = null)
{
Attachment image = new Attachment(imageAttachmentPath);
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true; // email body will allow html elements
msg.From = new MailAddress(from, "Admin"); // setting the Sender Email ID
msg.To.Add(sendTo); // adding the Recipient Email ID
if (!string.IsNullOrEmpty(carbonCopy)) // add CC email ids if supplied.
msg.CC.Add(carbonCopy);
msg.Subject = subject; //setting email subject and body
msg.Body = body;
msg.Attachments.Add(image);
//create a Smtp Mail which will automatically get the smtp server details
//from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Host = "smtp.gmail.com";
SmtpMail.Port = 587;
SmtpMail.EnableSsl = true;
SmtpMail.UseDefaultCredentials = false;
SmtpMail.Credentials = new System.Net.NetworkCredential(from, "password");
// sending the message.
try
{
SmtpMail.Send(msg);
}
catch (Exception ex) { }
}
答案 0 :(得分:0)
您的图片SRC属性很可能不是绝对的,可公开访问的URI。任何文件系统或本地URI都不会在电子邮件中显示图像。
具体来说,这些不起作用:
c://test.png
test.png
/folder/test.png
http://localhost/test.png
http://internaldomain/test.png
确保您的图片网址
http://
的协议开始)答案 1 :(得分:-1)
尝试this thread中的代码部分:
// creating the attachment
System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"c:\\test.png");
inline.ContentDisposition.Inline = true;
// sending the message
MailMessage email = new MailMessage();
// set the information of the message (subject, body ecc...)
// send the message
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
smtp.Send(email);
email.Dispose();