Created
June 17, 2014 17:12
-
-
Save antoniocachuan/607334cf16742ff7392a to your computer and use it in GitHub Desktop.
Sending a mail with Java Mail API (Just Run!!)
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
| /* | |
| * 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