Sending emails with Java

I start writing this post as a simple “how to send an email” using Java, but later I found I need to briefly explain more things. So, here is this kind of all in one summary about sending emails with Java.

Outside the Java SE platform, but included in JavaEE one, the JavaMail package provides a platform to build mail and messaging applications. Lets go with an example.

Sending a simple text message

Alternatively, instead using:

you can use next to set the message content:

Checking an email address

Here is a little trick to check, using a regular expression, if an email address is well formed:

Multipart messages

That’s fine, but usually you don’t send simple text messages. Instead you send nice HTML body messages with bold or italic text, images, and so on.

NOTE: See below at references section to see about MIME format which extends the data you can attach to an email to allow multiparts, attachments, etc.

When you write a multipart message the content is composed of different parts, for example one part is the message written as simple text and a second part with the same message written in an enhanced way using HTML. Then the client that reads the message is responsible to render the appropriate part depending on its capabilities.

 

Sending attachments

Terrific, we know how to send a plain text email and something more incredible like a multipart message with HTML content. Next step is to send an email attaching too some files.

Create an email with attached file is similar to create a multipart message where one part can be the text of the message and another part is the attached file. The secret is in the next lines:

 

HTML messages

Create a message o multipart message with HTML content is really easy, simply specify the MIME type in the setContent method:

Attaching images within the HTML code

If you write a rich message using HTML you can, of course, add images using the ‘img‘ tag. If the image is referenced from an external server there is no problem, but: how to attach an image to the message and render within the HTML message body?

The idea is as follow:

  • first you need to attach the image file and set an identifier and
  • second you need to write your HTML code and reference the image identifier in the ‘img‘ tag.

Anything more to say?

Arrived to this point you are almost a master of sending emails. You know how to send simple emails, multipart emails with richest HTML content and attach files and images on your message.

What more can a programmer desire?

Probably, a more easy to use API and that is what Apache Commons Email project offer you. See the ‘user guide‘ section http://commons.apache.org/email/userguide.html to understand what I say. It offers a more abstract API more close to humans than to protocols.

References

JavaMail – JavaMail project home page.

Apache Commons Email – Apache Commons subproject to simplify the way to work with JavaMail API. See the ‘user guide‘ section http://commons.apache.org/email/userguide.html.

MIME (Multipurpose Internet Mail Extensions) – Description of MIME format for multipart emails.

6 Responses

  1. Poornerd October 11, 2011 / 22:18

    What I really want to know is how to “send lots of emails quickly with Java”. How about things like connection pooling the SMTP connection? Any other tips?

  2. Luis Daniel Mesa February 18, 2012 / 22:20

    I’ve been trying to adapt the same trick to send html5 with a video tag, but it seems I haven’t been able to make it work.

  3. Andrey May 8, 2014 / 13:57

    In some case when I’m trying to send email using JavaMail, I can’t get youtube link, it’s sent free space, no link, no picture.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">