[英]Image not showing in gmail mail body after sending it from asp.net c# net.mail as mail.body
我需要將通過電子郵件生成的二維碼發送到他的gmail帳戶。 我調試了代碼,並用html可視化工具進行了檢查,qrcode顯示正確,但在gmail消息中看不到它
public void generate_qrcode()
{
try
{
string imgurl;
string code = txtCode.Text;
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
imgBarCode.Height = 150;
imgBarCode.Width = 150;
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
imgurl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
//plBarCode.Controls.Add(imgBarCode);
}
SendMail(imgurl);
}
catch (Exception ex)
{
}
}
public void SendMail(String imgurl)
{
string body = "Hello ,<br /><br />Please find your QRcode below<br /><br /><img src=' " + imgurl + " ' height='100' width='100'/><br/><br/>Thanks...";
SmtpClient Smtp_Server = new SmtpClient();
MailMessage e_mail = new MailMessage();
Smtp_Server.UseDefaultCredentials = false;
Smtp_Server.Credentials = new System.Net.NetworkCredential("samplemail527@gmail.com", "Sample527");
Smtp_Server.Port = 587;
Smtp_Server.EnableSsl = true;
Smtp_Server.Host = "smtp.gmail.com";
e_mail = new MailMessage();
e_mail.From = new MailAddress("samplemail527@gmail.com");
e_mail.To.Add(txtCode.Text);
e_mail.Subject = "Email Sending";
e_mail.IsBodyHtml = true;
e_mail.Body = body;
Smtp_Server.Send(e_mail);
}
大多數電子郵件閱讀器當前不支持Base64圖像。 非常不幸 您需要生成一個實際的圖像,並將其附加到具有唯一ID(例如GUID)的消息上,然后將該ID與CID前綴一起用作圖像標簽的src。
<img src="cid:GeneratedUniqueId" alt="Your QR Code" />
這是在電子郵件中嵌入圖像的良好參考。 發送帶有嵌入式圖像的電子郵件
也許您應該嘗試使用AlternateView。 您必須為資源分配一個ID,並在HTML <img>
標記內使用該ID。 src
屬性應解決此ID,就像這樣:
<img src="cid:ResourceId" />
不要忘記將鏈接的資源添加到備用視圖。
這是我使用的完整代碼:
Byte[] iconBytes = Convert.FromBase64String(@"iVBOR IMAGE BYTES Hy4vAAN==");
System.IO.MemoryStream iconBitmap = new System.IO.MemoryStream(iconBytes);
LinkedResource iconResource = new LinkedResource(iconBitmap, MediaTypeNames.Image.Jpeg);
iconResource.ContentId = "Icon";
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.To.Add(new MailAddress("recipient@domain.com", "Recipient Name"));
msg.From = new MailAddress("sender@domain.com", "Sender Name");
msg.Subject = "Attach image to mail";
string htmlBody = @"<html><head>";
htmlBody += @"<style>";
htmlBody += @"body{ font-family:'Calibri',sans-serif; }";
htmlBody += @"</style>";
htmlBody += @"</head><body>";
htmlBody += @"<h1>The attached image is here below</h1>";
htmlBody += @"<img src='cid:" + iconResource.ContentId + @"'/>";
htmlBody += @"</body></html>";
AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternativeView.LinkedResources.Add(iconResource);
msg.AlternateViews.Add(alternativeView);
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Port = 25; // You can use Port 25 if 587 is blocked
client.Host = "smtp.yourhost.com";
client.Send(msg);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.