Wednesday, 25th April 2012
Follow WikiJava on twitter now. @Wikijava

Send Simple email

From WikiJava

Jump to: navigation, search


this example shows how to send a simple email

Contents

the article

This example uses the JavaMail library to send an email with only plain text

SimpleMail

import javax.mail.*;
import javax.mail.internet.*;
 
import java.util.Properties;
 
class SimpleMail {
    public static void main(String[] args) throws Exception{
      Properties props = new Properties();
      props.setProperty("mail.transport.protocol", "smtp");
      props.setProperty("mail.host", "mymail.server.org");
      props.setProperty("mail.user", "emailuser");
      props.setProperty("mail.password", "");
 
      Session mailSession = Session.getDefaultInstance(props, null);
      Transport transport = mailSession.getTransport();
 
      MimeMessage message = new MimeMessage(mailSession);
      message.setSubject("Testing javamail plain");
      message.setContent("This is a test", "text/plain");
      message.addRecipient(Message.RecipientType.TO,
           new InternetAddress("elvis@presley.org"));
 
      transport.connect();
      transport.sendMessage(message,
          message.getRecipients(Message.RecipientType.TO));
      transport.close();
    }
}

See Also

send HTML Email, send Email with attachment, http://java.sun.com/products/javamail/FAQ.html, Real's Java How To

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.