2017-03-07 46 views
1

目前首先,我創建了一個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。

回答

-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")); 

如果圖像是一個BufferedImage,那麼你需要檢查使用ClassPathResource的構造方法或API爲addInline MimeMessageHelper

+0

爲什麼我需要添加' 「../../文件\\ \\上傳錯誤\\ \\ kindEditorImage 20170109155435.jpg」 我'那圖像文件的路徑? – user3441151

+0

@ user3441151你應該用你的圖片路徑替換'../../file\\upload\\bugs\\kindEditorImage\\20170109155435.j pg',我只是想給你舉個例子,因爲我確實不知道你的圖像路徑是什麼,你也可以用inputstream來替換文件的位置,這取決於你 – lucumt

+0

我覺得你沒有正確的讀我的問題。我不想爲QR碼創建任何圖像文件。我只是想直接將這個內聯圖像發送到電子郵件。那可能嗎? – user3441151

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

感謝您試圖在StackOverflow中提供幫助。但請參加[旅遊]。如果你用一些解釋來擴充你的代碼,你的答案會更好。 – Yunnosch

相關問題