JavaMail - Sending with Authentication


/ Published in: Java
Save to your folder(s)

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)


Copy this code and paste it in your HTML
  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. }
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.