Advertisement
tmax

Untitled

Sep 11th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import javax.mail.*;
  2. import javax.mail.internet.*;
  3. import javax.mail.Authenticator;
  4. import javax.mail.PasswordAuthentication;
  5. import java.util.Properties;
  6.  
  7. public class SimpleMail {
  8.  
  9.     private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
  10.     private static final String SMTP_AUTH_USER = "sendgrid-username";
  11.     private static final String SMTP_AUTH_PWD  = "sendgrid-password";
  12.  
  13.     public static void main(String[] args) throws Exception{
  14.        new SimpleMail().test();
  15.     }
  16.  
  17.     public void test() throws Exception{
  18.         Properties props = new Properties();
  19.         props.put("mail.transport.protocol", "smtp");
  20.         props.put("mail.smtp.host", "smtp.live.com");
  21.         props.put("mail.smtp.port", 587);
  22.         props.put("mail.smtp.auth", "true");
  23.  
  24.         Authenticator auth = new SMTPAuthenticator();
  25.         Session mailSession = Session.getDefaultInstance(props, auth);
  26.         mailSession.setDebug(true);
  27.         Transport transport = mailSession.getTransport();
  28.  
  29.         MimeMessage message = new MimeMessage(mailSession);
  30.  
  31.         Multipart multipart = new MimeMultipart("alternative");
  32.  
  33.         BodyPart part1 = new MimeBodyPart();
  34.         part1.setText("Checking to see what box this mail goes in ?");
  35.  
  36.         BodyPart part2 = new MimeBodyPart();
  37.         part2.setContent("Checking to see what box this mail goes in ?", "text/html");
  38.  
  39.         multipart.addBodyPart(part1);
  40.         multipart.addBodyPart(part2);
  41.  
  42.         message.setContent(multipart);
  43.         message.setFrom(new InternetAddress("nati_362@hotmail.com"));
  44.         message.setSubject("Tema del EMAIL");
  45.         message.addRecipient(Message.RecipientType.TO, new InternetAddress("nataliahang18@gmail.com"));
  46.  
  47.         transport.connect();
  48.         transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
  49.         transport.close();
  50.     }
  51.  
  52.     private class SMTPAuthenticator extends javax.mail.Authenticator {
  53.         public PasswordAuthentication getPasswordAuthentication() {
  54.            String username = "usuario de tu correo";
  55.            String password = "clave de tu correo";
  56.            return new PasswordAuthentication(username, password);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement