Java letters - attachments && embedded images

My problem was resolved this morning: Java Mail, sending multiple attachments does not work

This time I have a slightly more complicated problem: I would like to combine the attached files with the images.

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

When I open an email in Gmail, everything is fine: I have two attachments, and the image is displayed in the mail content (in the img tag).

The problem is with Thunderbird and with RoundCubic webmail: each one shown as an image is missing and displays it at the bottom as an attachment.

How can I do this job?

+6
source share
2 answers

The use of ImageHtmlEmail from the org.apache.commons.mail library is also pretty convincing. (UPDATE: it is only included in snapshot 1.3) For example:

  HtmlEmail email = new ImageHtmlEmail(); email.setHostName("mail.myserver.com"); email.addTo(" jdoe@somewhere.org ", "John Doe"); email.setFrom(" me@apache.org ", "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); 
+2
source

So, I got this by resolving javamail and using the spring shell:

http://static.springsource.org/spring/docs/1.2.x/reference/mail.html

I don’t know what magic he is doing in the background, but it works.

Thanks everyone!

+1
source

Source: https://habr.com/ru/post/906666/


All Articles