Skip to content

Instantly share code, notes, and snippets.

@MinCha
Created November 29, 2013 02:29
Show Gist options
  • Save MinCha/7700804 to your computer and use it in GitHub Desktop.
Save MinCha/7700804 to your computer and use it in GitHub Desktop.
Java SMTP
public class SMTPTest {
private final String from = "some@sender.com";
@Test
public void shouldSendMail() throws MessagingException {
mailTo("some@receiver.com");
}
private void mailTo(String to) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from, "sender name"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Title");
message.setText("Contents");
Transport.send(message);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment