0

I'm developing a room booking website using ASP.NET Core MVC. The requirement is that every time when customer do the booking, the system will assign a unique booking Id and generate the QR Code for that Unique booking number and send an email with that QR Code (I'm using HTML Email Template). The customer will come to the staff and scan the QR Code for further process. I've generated the unique number, created a QR Code but failed to send this Code in Email. I'm able to send that QR code from my controller to view, but When I attach that QR code, It does not show in the Email.

I've read some of the questions on Google, most of the people say that we have to save that generated QR Code in the Server directory and then send its reference in the email. But I was wondering that If I have a thousand bookings in a month, then my server has to have one thousand QR code images in a folder and later on every day the number of images will be getting increased.

I can save images on the server and send in an email, but I wanted to know what is the best practice to achieve this task because many companies are already using this functionality. Any suggestions, guidance will be appreciated.

The Practice Code that works fine for generating QR Code Controller Code:

Random r = new Random();
        int number = r.Next(10, 100000);
        using (MemoryStream ms = new MemoryStream())
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(number.ToString(), QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                bitMap.Save(ms, ImageFormat.Png);
                ViewBag.QRCodeImage = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
                ViewBag.Number = number;
            }
        }

And the Testing View Code is

@if (ViewBag.QRCodeImage != null)
                {
                    <h3>Booking Number is @ViewBag.Number</h3>
                    <img src="@ViewBag.QRCodeImage" alt="" style="height:150px;width:150px" />
                }

Is is working fine and showing me the QR Code in my View

But When I'm sending this QR Code in Email it is not displaying in email.

Actual Code for Email in Controller:

 string qrImagePath = "";
                using (MemoryStream ms = new MemoryStream())
                {
                    QRCodeGenerator qrGenerator = new QRCodeGenerator();

                    QRCodeData qrCodeData = qrGenerator.CreateQrCode(number.ToString(), QRCodeGenerator.ECCLevel.Q);
                    QRCode qrCode = new QRCode(qrCodeData);
                    using (Bitmap bitMap = qrCode.GetGraphic(20))
                    {
                        bitMap.Save(ms, ImageFormat.Png);
                        qrImagePath = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
                        ViewBag.Number = number;
                    }
                }
                MailText = MailText.Replace("[qrCode]", qrImagePath);
                EmailHelper emailHelper = new EmailHelper();
                bool emailResponse = emailHelper.SendEmail(model.Email, "Booking", MailText);

Email Helper is sending email and I can see the newly sent email in my inbox but QR Code Picture is not displaying.

QR Code Image is not displaying in Email and I cannot see the src attribute of an image in the email.

If anyone can guide me, I'll be very thankful. Have a good day.

  • Are you converting the image to base64? There are many posts on why that won't necessarily work (some email clients support it, some don't). Search for 'html email base64 image' – Nathan Aug 01 '21 at 23:16
  • Yes, I've written the above code after searching. Many of them says, we have to save image on server and then reference it in html img src. – Smart Learner Aug 03 '21 at 07:49
  • But it's not just using it's mainly about referencing a jpg/png/gif, and not a base64 encoded image. Can you do that? https://www.caniemail.com/features/image-base64/ – Nathan Aug 03 '21 at 22:37
  • I am asking for recommendations or best practice. – Smart Learner Aug 04 '21 at 23:03
  • Ok well my comment is the coding best practice, but I don't have experience with what happens when you have thousands of images. – Nathan Aug 05 '21 at 00:59

0 Answers0