0

I am generating QR code in c# using the qrcoder library. I can generate the code with the information i want, but i would like to send it also through email.

Using following code i get the email, but it won't display the image of the QR code in it. And if I change the address in which I think the code its being saved it also give an error like this:

System.Runtime.InteropServices.ExternalException occurred A generic error occurred in GDI+.

Here is the code:

public void GenerateQR(string infoReserva)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(infoReserva, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                bitMap.Save(Server.MapPath("~/Images/qrcode.png"), ImageFormat.Png);
            }
          
            MailMessage mm = new MailMessage();
            mm.From = new MailAddress("name@exampple.comr");
            mm.Subject = "How to email self-generated QR code";
            mm.Body = " <html><body> <p> QR code as below</p> <p> <img src='http://localhost:44362//Images/qrcode.png' alt='QR Code'/></p> </body></html> ";
            mm.To.Add("name@exampple.com");
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential();
            NetworkCred.UserName = "name@exampple.comr";
            NetworkCred.Password = "-----";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);

        }

I have the code of my project organized in folder like this:

  • 1_Entites
  • 2_DataAcces
  • 3_Exceptions
  • 4_API
  • 5_WEBAPP

The first 4 folder are the backend of the aplications, and the folder named WebApp is in which i have this code and the front end of the app, in this folder i created the folder of Images Do you know what is the best way of sending this email with de qr code

Thanks in advance!

Attila
  • 3,206
  • 2
  • 31
  • 44
David
  • 1
  • 2
  • 1
    You create the QR code image on the sender side. The recipient will not be able to see it on their localhost. One way would be to embed the actual image data (instead of an URL) into the tag: https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html – Klaus Gütter Apr 27 '21 at 05:36
  • Take a look at [this question](https://stackoverflow.com/questions/18358534/send-inline-image-in-email). – Andy Apr 27 '21 at 05:40

1 Answers1

0

You’re still going to struggle no matter what you do because you’re trying to send an image and somewhere there will be a mail client that blocks it (linking to an image on your server is a classic spammer trick and nearly certain to be blocked, gmail blocks images sent as data url but accepts inline attached multipart, somewhere else you’ll find a device, probably a phone, that doesn’t accept multipart embedded but accepts data urls..); I’d ditch the idea of sending any graphical pictures at all in the email and switch to using a QR library that can output characters or html so you can form your QR using blocks of black and white (characters or html elements). Not an endorsement and no affiliation, but I know QRCoder can output character based QRs

Caius Jard
  • 72,509
  • 5
  • 49
  • 80