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.
If anyone can guide me, I'll be very thankful. Have a good day.