View RSS Feed

Java Mail

Sending Email Messages

Rate this Entry
by , 04-25-2012 at 09:28 PM (1287 Views)
A MimeMessage object is prepared by an application so that to send email. Email is sent along with static method send(), on Transport class. JavaMail session object is used to create the message. Transport and session work along with App Engine Mail service, with no extra configuration.

import java.util.Properties;
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.MimeMessage;
Java Code:
// ...
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "...";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("user@example.com", "Mr. User"));
            msg.setSubject("Your Example.com account has been activated");
            msg.setText(msgBody);
            Transport.send(msg);

        } catch (AddressException e) {
            // ...
        } catch (MessagingException e) {
            // ...
        }
Calls which are made to the mail service are basically asynchronous and hence are immediately returned. Recipient mail servers are contacted and messages are delivered by Mail service management. If problem is there to send messages to recipient, error messages will be sent to sender.

Submit "Sending Email Messages" to Facebook Submit "Sending Email Messages" to Digg Submit "Sending Email Messages" to del.icio.us Submit "Sending Email Messages" to StumbleUpon Submit "Sending Email Messages" to Google

Categories
Mail Message

Comments