Show TOC

Creating E-Mail MessagesLocate this document in the navigation structure

Context

E-mail messages are represented by javax.mail. internet.Mime Message objects. A MimeMessage object represents a Multipurpose Internet Mail Extensions (MIME) format message. It can have a header, footer, body, attachments, recipients and other attributes. They are represented by the corresponding classes in the Java Mail API.

You can add content to the message by using the corresponding methods in the message objects. For example:

  • setSubject - Sets the subject of the message

  • setContent - Sets the content of the message

  • setRecipients - Sets the recipients' addresses, implementing the TO type of recipients that is defined by the javax.mail.Message.RecipientType .

  • addFrom - Adds the addresses to the existing "From" attribute.

The InternetAddress class represents the e-mail address.

Procedure


  1. Create a new MimeMessage object, passing the session object as an argument to the constructor.

    This creates an initially empty message.

  2. Using the corresponding methods of the message object, add the necessary content to the message.

  3. Send the message.

    More information: Sending E-Mail Messages .

Example

The following example illustrates creating a simple e-mail message. Note that if you chose to use the JavaMail Client Service configuration properties "as is", you do not have to specify server settings in the application source code. This means that you can skip the last two lines in the example below - the default value of the mail.from property, which specifies the sender of the mail message, will be used.

MimeMessage message = new MimeMessage(ses);
message.setSubject("Test", "UTF-8");
message.setContent("This is test", "text/html;charset=UTF-8");
message.setRecipients(javax.mail.Message.RecipientType.TO, "test1@myHost.my");
InternetAddress[] to = new InternetAddress[]{new InternetAddress("bobby@myHost.my");}
message.addFrom(to);