Skip to content

Instantly share code, notes, and snippets.

@phatduckk
Created July 28, 2011 21:00
Show Gist options
  • Save phatduckk/1112543 to your computer and use it in GitHub Desktop.
Save phatduckk/1112543 to your computer and use it in GitHub Desktop.
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SimpleMail {
private static String HOST = "smtp.sendgrid.net";
private static String USER = "your_isername_here";
private static String PASSWORD = "your_password_here";
private static String PORT = "465";
private static String FROM = "[email protected]";
private static String TO = "[email protected]";
private static String STARTTLS = "true";
private static String AUTH = "true";
private static String DEBUG = "true";
private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
public static void main(String[] args) throws Exception {
new SimpleMail().test();
}
public void test() throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.user", USER);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props, null);
//Construct the mail message
MimeMessage message = new MimeMessage(session);
message.setText("body text");
message.setSubject("subject");
message.setFrom(new InternetAddress(FROM));
message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(TO));
message.saveChanges();
//Use Transport to deliver the message
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment