简体   繁体   English

从asp.net c#net.mail作为mail.body发送后,图像未显示在gmail邮件正文中

[英]Image not showing in gmail mail body after sending it from asp.net c# net.mail as mail.body

I need to send the qrcode generated from the email to his gmail account. 我需要将通过电子邮件生成的二维码发送到他的gmail帐户。 I debugged the code and checked with html visualizer and the qrcode is displaying correctly but cannot see it in gmail message 我调试了代码,并用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);
        }

图片未以gmail显示

Base64 images are currently not supported by most email readers. 大多数电子邮件阅读器当前不支持Base64图像。 Very unfortunate. 非常不幸 You'll need to generate an actual image and attach it to the message with a unique id (like a GUID) and then use that ID as the image tag's src along with the CID prefix. 您需要生成一个实际的图像,并将其附加到具有唯一ID(例如GUID)的消息上,然后将该ID与CID前缀一起用作图像标签的src。

<img src="cid:GeneratedUniqueId" alt="Your QR Code" />

Here is the good reference to embed image in an email. 这是在电子邮件中嵌入图像的良好参考。 Send Email with embedded Images 发送带有嵌入式图像的电子邮件

Maybe you should try to use AlternateView. 也许您应该尝试使用AlternateView。 You have to assign an Id to a resource and use that id within HTML <img> tag. 您必须为资源分配一个ID,并在HTML <img>标记内使用该ID。 The src attribute should address this Id.Like so: src属性应解决此ID,就像这样:

<img src="cid:ResourceId" />

Don't forget to add linked resource to alternate view. 不要忘记将链接的资源添加到备用视图。

Here is the full code I used: 这是我使用的完整代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM