Skip to content

Instantly share code, notes, and snippets.

@Sutil
Created December 10, 2013 12:37
Show Gist options
  • Select an option

  • Save Sutil/7889946 to your computer and use it in GitHub Desktop.

Select an option

Save Sutil/7889946 to your computer and use it in GitHub Desktop.
Envio de e-mail com código JAVA
private void sendEmail(String destinatario) {
final String username = "user@server.com.br";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.starttls.enable", "true"); use with gmail
props.put("mail.smtp.host", "mail.csrh.com.br");
props.put("mail.smtp.port", "587");
props.put("mail.imap.ssl.checkserveridentity", "false"); // is not necessary with gmail
props.put("mail.imap.ssl.trust", "*"); // is not necessary with gmail
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("user@server.com.br"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinatario));
message.setSubject("your subject");
message.setText("Your message");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment