目前首先我将一个QR码图像文件创建到特定位置,然后将此图像作为内嵌图像发送到电子邮件中。这工作正常。
但我不想为QR码创建任何图像文件。我只想将此内容直接发送到电子邮件中。 那可能吗?
我的代码如下:
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。
答案 0 :(得分:0)
String mailContext = "<img height=\"250\" width=\"250\"
src=\"data:image/png;base64, " + object.getBase64QrCode() + "\"/>"
message.setContent(mailContext, "text/html; charset=utf-8");
答案 1 :(得分:-1)
是的,你可以这样做,但你需要再添加一行,如下所示:
String htmlText = "Dear,<br /><br />Please find QR Code.<br /><br /><img src=\"cid:image\">";
MimeMessageHelper messageHelper = new MimeMessageHelper(message,MimeMessageHelper.MULTIPART_MODE_RELATED,"UTF-8");
//in messageHelp specify the location or inputstream of your image file
messageHelper.addInline("cid",new ClassPathResource("../../file\\upload\\bugs\\kindEditorImage\\20170109155435.jpg"));
如果您的Image是bufferedImage,那么您需要检查 ClassPathResource 的构造函数方法或 MimeMessageHelper 的 addInline 的API