Created
July 28, 2011 21:00
-
-
Save phatduckk/1112543 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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