The Quintessential Program to Send E-Mail [J2EE]

This example demonstrates the simplest program that will send a textual E-mail message to a single recipient.
import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendApp { public static void send(String smtpHost, int smtpPort, String from, String to, String subject, String content) throws AddressException, MessagingException { // Create a mail session java.util.Properties props = new java.util.Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.port", ""+smtpPort); Session session = Session.getDefaultInstance(props, null); // Construct the message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); msg.setText(content); // Send the message Transport.send(msg); } public static void main(String[] args) throws Exception { // Send a test message send("hostname", 25, "joe@smith.com", "sue@smith.com", "re: dinner", "How about at 7?"); } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.