Return to Snippet

Revision: 12917
at April 1, 2009 10:45 by mahome


Updated Code
import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
 
public class JavaMail 
{ 
  public static void postMail( String recipient, 
                               String subject, 
                               String message, String from ) 
    throws MessagingException 
  { 
    Properties props = new Properties(); 	
    props.put( "mail.smtp.host", "mySMTPHost" );
    props.put( "mail.smtp.auth","true");
	
    Authenticator authenticator = new Authenticator () {
	public PasswordAuthentication getPasswordAuthentication(){
		return new PasswordAuthentication("user" "password");
	}
    };
    
    Session session = Session.getDefaultInstance( props , authenticator); 
    Message msg = new MimeMessage( session ); 
    InternetAddress addressFrom = new InternetAddress( from ); 
    msg.setFrom( addressFrom ); 
    InternetAddress addressTo = new InternetAddress( recipient ); 
    msg.setRecipient( Message.RecipientType.TO, addressTo ); 
    msg.setSubject( subject ); 
    msg.setContent( message, "text/plain" );
    
    Transport.send( msg ); 
  } 
 
  public static void main( String[] args ) throws Exception 
  { 
    postMail( "[email protected]", 
              "mySubject", 
              "myContent", 
              "[email protected]"); 
  } 
}

Revision: 12916
at April 1, 2009 10:44 by mahome


Initial Code
import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
 
public class JavaMail 
{ 
  public static void postMail( String recipient, 
                               String subject, 
                               String message, String from ) 
    throws MessagingException 
  { 
    Properties props = new Properties(); 	
	props.put( "mail.smtp.host", "mySMTPHost" );
	props.put( "mail.smtp.auth","true");
	
	Authenticator authenticator = new Authenticator () {
		public PasswordAuthentication getPasswordAuthentication(){
			return new PasswordAuthentication("user" "password");
		}
	};
    
    Session session = Session.getDefaultInstance( props , authenticator); 
    Message msg = new MimeMessage( session ); 
    InternetAddress addressFrom = new InternetAddress( from ); 
    msg.setFrom( addressFrom ); 
    InternetAddress addressTo = new InternetAddress( recipient ); 
    msg.setRecipient( Message.RecipientType.TO, addressTo ); 
    msg.setSubject( subject ); 
    msg.setContent( message, "text/plain" );
    
    Transport.send( msg ); 
  } 
 
  public static void main( String[] args ) throws Exception 
  { 
    postMail( "[email protected]", 
              "mySubject", 
              "myContent", 
              "[email protected]"); 
  } 
}

Initial URL


Initial Description
Simple Mail Example.

Tags: SMTP, Mail, Java

[JavaMail (Sun)](http://java.sun.com/products/javamail/)
[JavaMail (Galileo)](http://openbook.galileocomputing.de/javainsel8/javainsel_18_012.htm#mj2a4110a634c82a3f676d8f87a50d6567)

Initial Title
JavaMail - Sending with Authentication

Initial Tags
mail, java

Initial Language
Java