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ć?
Program Microsoft Outlook 2010 nie zezwala już na wstawiane obrazy! Czy to dla ciebie ciężki problem? –
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
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