Skip to content

Instantly share code, notes, and snippets.

@antoniocachuan
Created June 17, 2014 17:12
Show Gist options
  • Select an option

  • Save antoniocachuan/607334cf16742ff7392a to your computer and use it in GitHub Desktop.

Select an option

Save antoniocachuan/607334cf16742ff7392a to your computer and use it in GitHub Desktop.
Sending a mail with Java Mail API (Just Run!!)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.webapp.util;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author Antonio Cachuan
*/
public class SendMail {
/**
* @param args the command line arguments
*/
public SendMail(){
}
public static void main(String[] args) {
// TODO code application logic here
SendMail M = new SendMail();
M.enviarCorreo();
System.out.println("FIN");
}
public void enviarCorreo() {
//public class SendMailTLS {
//public static void main(String[] args) {
final String username = "[email protected]";
final String password = "password";
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");//587
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("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Test OCBMAIL");
message.setText("Reporting:" + "\n\n It Works");
Transport.send(message);
System.out.println("Done");
} 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