avevo un problema risolto già questa mattina: Java Mail, sending multiple attachments not workingJava mail - gli allegati && immagini in linea
Questa volta ho un problema un po 'più complicato: vorrei combinare file allegati con le immagini.
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);
}
}
Quando apro l'e-mail in Gmail tutto va bene: ho le due allegati, e l'immagine viene visualizzata nel contenuto della posta (nel tag img).
Il problema è con Thunderbird e con webmail RoundCubic: ognuno mostra come l'immagine manca e lo visualizza in basso come allegato.
Come posso fare questo lavoro?
Microsoft Outlook 2010 non consente più immagini in linea! Questo è un grosso problema per te? –
Beh, non stavo usando Outlook ma Thunderbird. Prima ho cambiato la multipart da "related" a "mixed" stava funzionando. Pls. guarda la domanda di StackOverflow collegata. – dbalakirev
Quindi ho ottenuto questo risolto usando questo: http://static.springsource.org/spring/docs/1.2.x/reference/mail.html La primavera sta facendo il trucco in background. Ma non posso chiudere la domanda perché il mio rappresentante non è abbastanza alto. – dbalakirev