Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Adding functionality with email messages using struts app

How can I add functionality for sending email messages from our struts application?

The JavaMail API (http://java.sun.com/products/javamail) provides an easy-to-use framework for building email senders and receivers over many different protocols, such as IMAP, MAPI, POP, POP3, and SMTP. The JavaMail API package can be downloaded separately and is also available as part of the Java 2 Enterprise Edition (J2EE) platform.

The following steps illustrate how easy the JavaMail API makes it to send email messages:

1) Import the JavaMail APIs:

import javax.mail.*;

import javax.mail.internet.*;

import javax.mail.MessagingException;

2) Create the mail session:

Properties props = new Properties();

props.put("mail.host", _mailhost);

props.put("mail.user", _mailuser);

Session mailSession =

Session.getDefaultInstance(props, null);

3) Create the email message:

MimeMessage msg = new MimeMessage(mailSession);

msg.setFrom(new InternetAddress("jdoe@foobar.com",

"John Doe"));


new InternetAddress("jhanson@foobar.com",

"Jeff Hanson"));

msg.setSubject("This is the email message subject");

msg.setText("This is the email message body");

4) Send the message:


Dig Deeper on Topics Archive

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.