Advertisement
Guest User

java-webfaction-smtp

a guest
Feb 28th, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4.  
  5. public class SentEmail {
  6.  
  7.     private static String USER_NAME = ""; // The name of a WebFaction mailbox as it appears on the control panel
  8.     private static String PASSWORD = ""; // The mailbox password
  9.     private static String RECIPIENT = ""; // Whoever you wish to send an email to
  10.     private static String SENDER = ""; // An email address configured on the WebFaction control panel
  11.     private static String SUBJECT = ""; // The email's subject
  12.     private static String BODY = ""; // The email's body
  13.  
  14.     public static void main(String[] args) {
  15.         String user = USER_NAME;
  16.         String pass = PASSWORD;
  17.         String from = SENDER;
  18.         String[] to = { RECIPIENT };
  19.         String subject = SUBJECT;
  20.         String body = BODY;
  21.  
  22.         sendMail(user, pass, from, to, subject, body);
  23.     }
  24.  
  25.     private static void sendMail(String user, String pass, String from, String[] to, String subject, String body) {
  26.         Properties props = System.getProperties();
  27.         String host = "smtp.webfaction.com";
  28.         props.put("mail.smtp.starttls.enable", "true");
  29.         props.put("mail.smtp.host", host);
  30.         props.put("mail.smtp.user", user);
  31.         props.put("mail.smtp.password", pass);
  32.         props.put("mail.smtp.port", "587");
  33.         props.put("mail.smtp.auth", "true");
  34.  
  35.         Session session = Session.getDefaultInstance(props);
  36.         MimeMessage message = new MimeMessage(session);
  37.  
  38.         try {
  39.             message.setFrom(new InternetAddress(from));
  40.             InternetAddress[] toAddress = new InternetAddress[to.length];
  41.  
  42.             // To get the array of addresses
  43.             for( int i = 0; i < to.length; i++ ) {
  44.                 toAddress[i] = new InternetAddress(to[i]);
  45.             }
  46.  
  47.             for( int i = 0; i < toAddress.length; i++) {
  48.                 message.addRecipient(Message.RecipientType.TO, toAddress[i]);
  49.             }
  50.  
  51.             message.setSubject(subject);
  52.             message.setText(body);
  53.             Transport transport = session.getTransport("smtp");
  54.             transport.connect(host, user, pass);
  55.             transport.sendMessage(message, message.getAllRecipients());
  56.             transport.close();
  57.         }
  58.         catch (AddressException ae) {
  59.             ae.printStackTrace();
  60.         }
  61.         catch (MessagingException me) {
  62.             me.printStackTrace();
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement