2012-01-23 57 views
6

tôi đã là một vấn đề được giải quyết đã sáng nay: Java Mail, sending multiple attachments not workingJava mail - file đính kèm && hình ảnh inline

Lần này tôi có một vấn đề hơi phức tạp hơn: Tôi muốn kết hợp các file kèm theo hình ảnh.

import java.io.IOException; 
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class MailTest 
{ 

    public static void main(String[] args) throws AddressException, MessagingException, IOException 
    { 
     String host = "***"; 
     String from = "***"; 
     String to = "***"; 

     // Get system properties 
     Properties props = System.getProperties(); 

     // Setup mail server 
     props.put("mail.smtp.host", host); 

     // Get session 
     Session session = Session.getDefaultInstance(props, null); 

     // Define message 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
     message.setSubject("Hello JavaMail"); 

     // Handle attachment 1 
     MimeBodyPart messageBodyPart1 = new MimeBodyPart(); 
     messageBodyPart1.attachFile("c:/Temp/a.txt"); 

     // Handle attachment 2 
     MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 
     messageBodyPart2.attachFile("c:/Temp/b.txt"); 

     FileDataSource fileDs = new FileDataSource("c:/Temp/gti.jpeg"); 
     MimeBodyPart imageBodypart = new MimeBodyPart(); 
     imageBodypart.setDataHandler(new DataHandler(fileDs)); 
     imageBodypart.setHeader("Content-ID", "<myimg>"); 
     imageBodypart.setDisposition(MimeBodyPart.INLINE); 

     // Handle text 
     String body = "<html><body>Elotte<img src=\"cid:myimg\" width=\"600\" height=\"90\" alt=\"myimg\" />Utana</body></html>"; 

     MimeBodyPart textPart = new MimeBodyPart(); 
     textPart.setHeader("Content-Type", "text/plain; charset=\"utf-8\""); 
     textPart.setContent(body, "text/html; charset=utf-8"); 

     MimeMultipart multipart = new MimeMultipart("mixed"); 

     multipart.addBodyPart(textPart); 
     multipart.addBodyPart(imageBodypart); 
     multipart.addBodyPart(messageBodyPart1); 
     multipart.addBodyPart(messageBodyPart2); 

     message.setContent(multipart); 

     // Send message 
     Transport.send(message); 
    } 
} 

Khi tôi mở email trong Gmail mọi thứ đều ổn: Tôi có hai tệp đính kèm và hình ảnh được hiển thị trong nội dung thư (trong thẻ img).

Sự cố xảy ra với Thunderbird và với webmail RoundCubic: mỗi email hiển thị như hình ảnh bị thiếu và hiển thị nó ở dưới cùng dưới dạng tệp đính kèm.

Tôi làm cách nào để thực hiện công việc này?

+0

Microsoft Outlook 2010 không cho phép hình ảnh nội tuyến nữa! Đây có phải là vấn đề nặng nề cho bạn không? –

+0

Tôi cũng không sử dụng Outlook nhưng Thunderbird. Trước khi tôi thay đổi các multipart từ "liên quan" để "hỗn hợp" nó đã được làm việc. Xin lỗi. xem câu hỏi stackoverflow được liên kết. – dbalakirev

+0

Vì vậy, tôi đã sắp xếp điều này bằng cách sử dụng: http://static.springsource.org/spring/docs/1.2.x/reference/mail.html Mùa xuân đang thực hiện thủ thuật trong nền. Nhưng tôi không thể đóng câu hỏi vì đại diện của tôi không đủ cao. – dbalakirev

Trả lời

2

Khá kiên nhẫn cũng là việc sử dụng ImageHtmlEmail từ org.apache.commons.mail library. (CẬP NHẬT: nó chỉ được bao gồm trong ảnh chụp nhanh 1.3) Ví dụ:

HtmlEmail email = new ImageHtmlEmail(); 
    email.setHostName("mail.myserver.com"); 
    email.addTo("[email protected]", "John Doe"); 
    email.setFrom("[email protected]", "Me"); 
    email.setSubject("Test email with inline image"); 

    // embed the image and get the content id 
    URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); 
    String cid = email.embed(url, "Apache logo"); 

    // set the html message 
    email.setHtmlMsg(htmlEmailTemplate, new File("").toURI().toURL(), false); 
Các vấn đề liên quan