0

I have this piece of code that generates a QR code and what I am trying to do after I generate the QR code and display it in an email and send it off. I am able to generate the QR code and the email gets sent. My issue is the image of the QR code sometimes does not display in the email. Here is my testing I did...

Gmail - No Image

Yahoo - QR Code Image Appeared

Outlook - No Image

Hotmail - No Image

Apple Mail - No Image when I initially open the email, if I open it for a second time, the QR Code Image appears.

Mobile - QR Code Image Appeared.

Here is my code that generates the QR Code:

Random rand = new Random();

            string code = emailAddress + rand.Next();
            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);
                }

                var qrCode = code;
            }

and here is how I am sending the email...

var message += "<img src='" + imgBarCode.ImageUrl + "' />";

            using (SmtpClient client = new SmtpClient("xxx.xxx.x.xx"))
            {
                MailAddress to = new MailAddress(email);
                MailAddress from = new MailAddress("example@domain.com");
                MailMessage email = new MailMessage(from, to);
                email.Subject = "Subject";
                email.Body = message;
                email.IsBodyHtml = true;
                try
                {
                    client.Send(email);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }

What am I doing wrong? Why is my image in my email not appearing?

user979331
  • 11,039
  • 73
  • 223
  • 418
  • @Win Did you manually type that instead of actually voting to close? – DavidG Mar 16 '17 at 13:52
  • @DavidG If I mark as duplicate, it'll close the question with single vote. I try not to close with single vote, unless there is already a couple of votes. – Win Mar 16 '17 at 13:57
  • @Win Even if it's a clear dupe? – DavidG Mar 16 '17 at 13:58
  • @DavidG If it's a clear dupe, I do. I just want to confirm with others for this question. I don't want to hurt others for my careless mistake. :) – Win Mar 16 '17 at 14:05

0 Answers0