Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- public class SentEmail {
- private static String USER_NAME = ""; // The name of a WebFaction mailbox as it appears on the control panel
- private static String PASSWORD = ""; // The mailbox password
- private static String RECIPIENT = ""; // Whoever you wish to send an email to
- private static String SENDER = ""; // An email address configured on the WebFaction control panel
- private static String SUBJECT = ""; // The email's subject
- private static String BODY = ""; // The email's body
- public static void main(String[] args) {
- String user = USER_NAME;
- String pass = PASSWORD;
- String from = SENDER;
- String[] to = { RECIPIENT };
- String subject = SUBJECT;
- String body = BODY;
- sendMail(user, pass, from, to, subject, body);
- }
- private static void sendMail(String user, String pass, String from, String[] to, String subject, String body) {
- Properties props = System.getProperties();
- String host = "smtp.webfaction.com";
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", host);
- props.put("mail.smtp.user", user);
- props.put("mail.smtp.password", pass);
- props.put("mail.smtp.port", "587");
- props.put("mail.smtp.auth", "true");
- Session session = Session.getDefaultInstance(props);
- MimeMessage message = new MimeMessage(session);
- try {
- message.setFrom(new InternetAddress(from));
- InternetAddress[] toAddress = new InternetAddress[to.length];
- // To get the array of addresses
- for( int i = 0; i < to.length; i++ ) {
- toAddress[i] = new InternetAddress(to[i]);
- }
- for( int i = 0; i < toAddress.length; i++) {
- message.addRecipient(Message.RecipientType.TO, toAddress[i]);
- }
- message.setSubject(subject);
- message.setText(body);
- Transport transport = session.getTransport("smtp");
- transport.connect(host, user, pass);
- transport.sendMessage(message, message.getAllRecipients());
- transport.close();
- }
- catch (AddressException ae) {
- ae.printStackTrace();
- }
- catch (MessagingException me) {
- me.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement