有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

二维码Java将BuffereImage作为内联图像直接发送到电子邮件中

目前,我首先在特定位置创建了一个二维码图像文件,然后将该图像作为内联图像发送到电子邮件中。这工作正常

但我不想为二维码创建任何图像文件。我只想直接把这个作为内联图像发送到电子邮件中。 那有可能吗

我的代码如下

public class SendQR {
    public static void main(String[] args) {
        String qrCodeText = "QR Code for Test";
        String filePath = "G:\\file\\qrTest.png";
        int size = 125;
        String fileType = "png";
        File qrFile = new File(filePath);
        createQRImage(qrFile, qrCodeText, size, fileType);
        SendingEmailRegistration(filePath);
    }

    private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType) {
        try {
            Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
            hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hintMap.put(EncodeHintType.MARGIN, 1);
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
            int CrunchifyWidth = byteMatrix.getWidth();
            BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
            image.createGraphics();

            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
            graphics.setColor(Color.BLACK);

            for (int i = 0; i < CrunchifyWidth; i++) {
                for (int j = 0; j < CrunchifyWidth; j++) {
                    if (byteMatrix.get(i, j)) {
                        graphics.fillRect(i, j, 1, 1);
                    }
                }
            }
            ImageIO.write(image, fileType, qrFile);
        } catch (WriterException e) {
            System.out.println("WriterException inside createQRImage : " + e);
        } catch (IOException e1) {
            System.out.println("IOException inside createQRImage : " + e1);
        }
    }

    public static boolean SendingEmailRegistration(String file) {
        boolean status = true;
        String username = "no-reply@test.com";
        String password = "test@123";
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", 465);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", 465);

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("tuser@test.com"));
            message.setSubject("Wonderhood: Registration Completed");
            MimeMultipart multipart = new MimeMultipart("related");
            BodyPart messageBodyPart = new MimeBodyPart();
            String htmlText = "Dear,<br /><br />Please find QR Code.<br /><br /><img src=\"cid:image\">";
            messageBodyPart.setContent(htmlText, "text/html");
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            DataSource fds = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(fds));
            messageBodyPart.setHeader("Content-ID", "<image>");
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            Transport.send(message);
            System.out.println("Email sent successfully");
        } catch (MessagingException e) {
            status = false;
        }
        return status;
    }
}

我正在使用Java


共 (1) 个答案

  1. # 1 楼答案

    String mailContext = "<img height=\"250\" width=\"250\" 
    src=\"data:image/png;base64, " + object.getBase64QrCode() + "\"/>"
    message.setContent(mailContext, "text/html; charset=utf-8");