2016-02-04 94 views
1

我使用以下代碼與圖像一起發送電子郵件,並且它完美地工作。當我發送圖像時,它會顯示在主體中,而不是附加在電子郵件中。所以,我可以輕鬆地從附件下載該圖像。使用JavaMail API發送帶有嵌入式圖像的電子郵件

有什麼方法可以在電子郵件中的附件圖像中添加圖像,而不是在圖像中的圖像?

package com.tutorialspoint; 

import java.util.Properties; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class SendInlineImagesInEmail 
{ 
    public static void main(String[] args) 
    { 
     // Recipient's email ID needs to be mentioned. 
     String to = "[email protected]"; 

     // Sender's email ID needs to be mentioned 
     String from = "[email protected]"; 
     final String username = "manishaspatil"; //change accordingly 
     final String password = "******"; //change accordingly 

     // Assuming you are sending email through relay.jangosmtp.net 
     String host = "relay.jangosmtp.net"; 

     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.port", "25"); 

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

     try { 
      // Create a default MimeMessage object. 
      Message message = new MimeMessage(session); 

      // Set From: header field of the header. 
      message.setFrom(new InternetAddress(from)); 

      // Set To: header field of the header. 
      message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse(to)); 

      // Set Subject: header field 
      message.setSubject("Testing Subject"); 

      // This mail has 2 part, the BODY and the embedded image 
      MimeMultipart multipart = new MimeMultipart("related"); 

      // first part (the html) 
      BodyPart messageBodyPart = new MimeBodyPart(); 
      String htmlText = "<H1>Hello</H1><img src=\"cid:image\">"; 
      messageBodyPart.setContent(htmlText, "text/html"); 
      // add it 
      multipart.addBodyPart(messageBodyPart); 

      // second part (the image) 
      messageBodyPart = new MimeBodyPart(); 
      DataSource fds = new FileDataSource(
       "/home/manisha/javamail-mini-logo.png"); 

      messageBodyPart.setDataHandler(new DataHandler(fds)); 
      messageBodyPart.setHeader("Content-ID", "<image>"); 

      // add image to the multipart 
      multipart.addBodyPart(messageBodyPart); 

      // put everything together 
      message.setContent(multipart); 
      // Send message 
      Transport.send(message); 

      System.out.println("Sent message successfully...."); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 
+0

你有問題嗎? –

+0

是的,而不是在身體添加圖像是否有任何方式附加在郵件中的圖像? –

+0

你是否希望自己的形象只是一個附件? –

回答

1

要添加圖像作爲附件,我正在使用FileDataSource

// Set a file as an attachment 
    public static void setFileAsAttachment(Message msg, String filename) 
      throws MessagingException { 

     // Create and fill first part 
     MimeBodyPart p1 = new MimeBodyPart(); 
     p1.setText("This is part one of a test multipart e-mail." + 
        "The second part is file as an attachment"); 

     // Create second part 
     MimeBodyPart p2 = new MimeBodyPart(); 

     // Put a file in the second part 
     FileDataSource fds = new FileDataSource(filename); 
     p2.setDataHandler(new DataHandler(fds)); 
     p2.setFileName(fds.getName()); 

     // Create the Multipart. Add BodyParts to it. 
     Multipart mp = new MimeMultipart(); 
     mp.addBodyPart(p1); 
     mp.addBodyPart(p2); 

     // Set Multipart as the message's content 
     msg.setContent(mp); 
    } 

並通過您的圖片路徑,

setFileAsAttachment(msg, "<path here>"); 
+0

歡迎:)很高興它幫助你 –

相關問題