How to Generate QR code and send email using java

Here's an example Java program that generates a QR code and sends it as the body of an email using JavaMail: Add below dependency in pom.xml or download jar and add to the project
 
  <dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.1</version>
   </dependency>   
This is the main class, where we can generate QR code and send email
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.imageio.ImageIO;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeEmailExample {

    public static void main(String[] args) throws Exception {
        String data = "https://example.com"; // the data to encode in the QR code
        int width = 300; // the width of the QR code
        int height = 300; // the height of the QR code

        BitMatrix bitMatrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, width, height);
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        byte[] imageData = baos.toByteArray();

        String from = "sender@example.com"; // the email address of the sender
        String to = "recipient@example.com"; // the email address of the recipient
        String subject = "QR code"; // the subject of the email
        String body = "Please scan the QR code below:"; // the body of the email

        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.example.com"); // the SMTP server to use
        Session session = Session.getDefaultInstance(properties);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(body, "text/html");

        MimeBodyPart attachmentBodyPart = new MimeBodyPart();
        attachmentBodyPart.setDataHandler(new DataHandler
            (new ByteArrayDataSource(imageData, "image/png")));
        attachmentBodyPart.setFileName("qrcode.png");

        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        multipart.addBodyPart(attachmentBodyPart);

        message.setContent(multipart);

        Transport.send(message);

        System.out.println("Email sent successfully.");
    }
}
We can use below one also to send qr code in email
String base64Encoded = Base64.getEncoder().encodeToString(imageData);
String htmlBody = "<html><body><h1>QR Code Example</h1><img src=\"data:image/png;base64," + base64Encoded + "\"/></body></html>";
   // Set mail server properties
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        // Create a mail session with the specified properties
        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("your_email@gmail.com", "your_password");
            }
        });
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("QR Code Example");
message.setContent(htmlBody, "text/html");
Transport.send(message);
Make sure to replace the example values with your own values for the email addresses, SMTP server, and other details. Also, you'll need to add the required dependencies to your project's classpath, such as JavaMail and ZXing.

Comments

Popular posts from this blog

What is java?

How to Get started with Power BI

What are the different concepts in Java?