Posted By

mahome on 04/01/09


Tagged

mail java smtp


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

appx
LarryStreeter


JavaMail - Sending with Authentication


 / Published in: Java
 

Simple Mail Example.

Tags: SMTP, Mail, Java

JavaMail (Sun) JavaMail (Galileo)

  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4.  
  5. public class JavaMail
  6. {
  7. public static void postMail( String recipient,
  8. String subject,
  9. String message, String from )
  10. throws MessagingException
  11. {
  12. Properties props = new Properties();
  13. props.put( "mail.smtp.host", "mySMTPHost" );
  14. props.put( "mail.smtp.auth","true");
  15.  
  16. Authenticator authenticator = new Authenticator () {
  17. public PasswordAuthentication getPasswordAuthentication(){
  18. return new PasswordAuthentication("user" "password");
  19. }
  20. };
  21.  
  22. Session session = Session.getDefaultInstance( props , authenticator);
  23. Message msg = new MimeMessage( session );
  24. InternetAddress addressFrom = new InternetAddress( from );
  25. msg.setFrom( addressFrom );
  26. InternetAddress addressTo = new InternetAddress( recipient );
  27. msg.setRecipient( Message.RecipientType.TO, addressTo );
  28. msg.setSubject( subject );
  29. msg.setContent( message, "text/plain" );
  30.  
  31. Transport.send( msg );
  32. }
  33.  
  34. public static void main( String[] args ) throws Exception
  35. {
  36. postMail( "[email protected]",
  37. "mySubject",
  38. "myContent",
  39. "[email protected]");
  40. }
  41. }

Report this snippet  

You need to login to post a comment.