Wednesday, May 02, 2007

How To Send E-Mail By JavaMail API

This example demonstrates the simplest program that will send a textual E-mail message to a single recipient.

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {
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("", 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));

// Send the message

public static void main(String[] args) throws Exception {
// Send a test message
SendMail sender = new SendMail();
sender.send("hostname", 25, "", "", "javamail example ", "How to send mail by javamail API?");


Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By & Programming Blogs - BlogCatalog Blog Directory