Send an email message : Email Message « Email « Java






Send an email message

 
 
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.util.Properties;

public class Main {
  public static void main(String[] args) {
    String from = "user@some-domain.com";
    String to = "user@some-domain.com";
    String subject = "Hi There...";
    String text = "How are you?";

    Properties properties = new Properties();
    properties.put("mail.smtp.host", "smtp.some-domain.com");
    properties.put("mail.smtp.port", "25");
    Session session = Session.getDefaultInstance(properties, null);

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    message.setText(text);

    Transport.send(message);
  }
}

   
  








Related examples in the same category

1.Creates a message, retrieves a Transport from the session based on the type of the address and sends the message
2.Create a very simple text/plain message and sends it
3.Show information about and contents of messages
4.Get Message Size Line Count
5.Access message given its UID and Show information about and contents of messages
6.Create a simple multipart/mixed message and sends it
7.How to construct and send an RFC822 (singlepart) message
8.How to construct and send a single part html message