目前首先,我創建了一個QR碼圖像文件到一個特定的位置,然後我將這個圖像作爲嵌入式圖像發送到電子郵件。這工作正常。Java將BufferedImage直接發送到電子郵件作爲嵌入圖像
但我不想爲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 = "[email protected]";
String password = "[email protected]";
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("[email protected]"));
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。
爲什麼我需要添加' 「../../文件\\ \\上傳錯誤\\ \\ kindEditorImage 20170109155435.jpg」 我'那圖像文件的路徑? – user3441151
@ user3441151你應該用你的圖片路徑替換'../../file\\upload\\bugs\\kindEditorImage\\20170109155435.j pg',我只是想給你舉個例子,因爲我確實不知道你的圖像路徑是什麼,你也可以用inputstream來替換文件的位置,這取決於你 – lucumt
我覺得你沒有正確的讀我的問題。我不想爲QR碼創建任何圖像文件。我只是想直接將這個內聯圖像發送到電子郵件。那可能嗎? – user3441151