2012-01-23 9 views
6

miałem problem rozwiązany już dziś rano: Java Mail, sending multiple attachments not workingJava mail - załączniki && inline obrazy

Tym razem mam nieco bardziej skomplikowany problem: Chciałbym połączyć załączonych plików z obrazami.

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

Podczas otwierania wiadomości e-mail w Gmailu wszystko jest w porządku: Mam dwa załączniki, a obraz jest wyświetlany w treści maila (w znaczniku img).

Problem dotyczy programu Thunderbird i poczty internetowej RoundCubic: każda z nich jest podobna do obrazu, a na dole jest wyświetlana jako załącznik.

Jak mogę to ułatwić?

+0

Program Microsoft Outlook 2010 nie zezwala już na wstawiane obrazy! Czy to dla ciebie ciężki problem? –

+0

Cóż, nie korzystałem z Outlooka, ale z Thunderbirda. Przed zmianą wieloczęściowy z "pokrewny" na "mieszany" działał. Pls. zobacz powiązane pytanie stackoverflow. – dbalakirev

+0

Więc mam to załatwione za pomocą tego: http://static.springsource.org/spring/docs/1.2.x/reference/mail.html Wiosna robi lewę w tle. Ale nie mogę zamknąć pytania, ponieważ mój przedstawiciel nie jest wystarczająco wysoki. – dbalakirev

Odpowiedz

2

Dość wygodne jest również użycie ImageHtmlEmail z poziomu org.apache.commons.mail library. (AKTUALIZACJA: jest uwzględniona tylko w migawce 1.3) Na przykład:

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); 
Powiązane problemy