Created
November 29, 2013 02:29
-
-
Save MinCha/7700804 to your computer and use it in GitHub Desktop.
Java SMTP
This file contains 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
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