Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I worked with an e-mail service implementation I used a cool hint. So if you use <code>MimeMessage</code> too, and want just check if the message is formatted as expected, checking if attachments are there, HTML is well formatted, images are right referenced and so on, you could build the entire message, and during debug you could have some code like this:</p> <pre><code>MimeMessage msg = new MimeMessage(session); ... if ("1".equals(System.getProperty("mail.debug"))) { msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml"))); } </code></pre> <p>Every time this is executed the <code>MimeMessage</code> instane will be saved to <code>emailSent.eml</code>. This file you can open with your e-mail reader and check if everything is fine.</p> <p>Of course that you need to execute your application with <em>-Dmail.debug=1</em> parameter.</p> <p>An example with attached file, text message and html message with this approach could be like this:</p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; 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.Multipart; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.junit.Test; public class MimeMessageTest { @Test public void tesstMimeMessage() throws MessagingException, FileNotFoundException, IOException { Session session = Session.getDefaultInstance(new Properties(), null); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("admin@foo.bar", "Foo Admin")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("baz@foo.bar", "Baz User")); msg.setSubject("Subject from admin e-mail to baz user"); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText("test message and so on"); mbp1.setContent("&lt;h1&gt;test message and so on in HTML&lt;/h1&gt;", "text/html"); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource("/tmp/fileToBeAttached"); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); if ("1".equals(System.getProperty("debug"))) { msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml"))); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload