2017-12-24 6 views
0

이미지를 전자 메일로 보낼 수는 있지만 이미지는 noname이라는 파일로 제공됩니다 (어쨌든, Gmail에서) . 전자 메일에 첨부 할 이미지의 파일 이름을 어떻게 변경합니까? javax.mail.internet.MimeBodyPart.setFileName(String)에서javax.mail.jar를 사용하여 전자 메일에 첨부 된 이미지에 파일 이름을 추가하는 방법

public void SendEmail(
      String smtp_host_, 
      String smtp_port_, 
      String smtp_username_, 
      String smtp_password_, 
      String to_, 
      String subject_, 
      String body_, 
      String image_path_) { 
     Properties props = new Properties(); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.host", smtp_host_); 
     props.put("mail.smtp.port", smtp_port_); 

     Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(smtp_username_, smtp_password_); 
      } 
      }); 

     try { 

      Message message = new MimeMessage(session); 

      message.setSubject(subject_); 
      message.setFrom(new InternetAddress(smtp_username_)); 
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to_)); 

      MimeMultipart multipart = new MimeMultipart("related"); 

      BodyPart messageBodyPart = new MimeBodyPart(); 
      messageBodyPart.setContent(body_, "text/html"); 
      multipart.addBodyPart(messageBodyPart); 

      messageBodyPart = new MimeBodyPart(); 
      DataSource fds = new FileDataSource(image_path_); 
      messageBodyPart.setDataHandler(new DataHandler(fds)); 
      messageBodyPart.setHeader("Content-ID","<image>"); 
      multipart.addBodyPart(messageBodyPart); 

      message.setContent(multipart); 

      Transport transport = session.getTransport(); 
      transport.connect(); 
      transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); 
      transport.close(); 
     } 
     catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 
+1

에게 FileDataSource에 대한 속기 및 setDataHandler 호출합니다 ([attachFile] https://docs.oracle.com입니다 /javaee/7/api/javax/mail/internet/MimeBodyPart.html#attachFile-java.io.File-) 메소드를 사용하면 body 부분에 이름을 설정해야한다. – VGR

답변

1

봐 : 여기

내가 이미지가 포함 된 이메일을 전송하기 위해 사용하고 코드의 섹션입니다.

당신은 부품을 만든 후 그것을 할 수 있습니다 :

messageBodyPart = new MimeBodyPart(); 
((MimeBodyPart) messageBodyPart).setFileName("filename.ext"); 

참조 자바 문서 here