Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java-JavaMail: How to send an email message

Java-JavaMail: How to send an email message
{filelink=1020}

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 SendEmail
 {
  public static void main(String[] args) throws Exception
  	{
    String from = "moi@domain.com";
    String to = "lui@domain.com";
    String subject = "Hello";
    String content = "Thank you..";  
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "smtp.serveur.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(content);
    message.setSentDate(new java.util.Date());
    // Send message
    Transport.send(message);
    System.out.println ("Message envoyé");
  }
}

No comments yet.

Leave a Comment