2017-03-07 1 views
1

현재 먼저 특정 위치에 하나의 QR 코드 이미지 파일을 만든 다음이 이미지를 인라인 이미지로 전자 메일로 보냅니다. 이것은 올바르게 작동하고 있습니다.자바 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; 
    } 
} 

아래에 주어진다.

답변

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

당신은 이미지, 당신은 의 생성자 메서드 ClassPathResource가 또는 API를 확인하는 BufferedImage에 필요한 경우

+0

나는'추가해야하는 이유 MimeMessageHelper에 대한 addInline의 "../../ 파일은 \\ 버그를 업로드 \\ \\ 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