Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. import java.util.Properties;
  2. import javax.mail.Address;
  3. import javax.mail.Message;
  4. import javax.mail.MessagingException;
  5. import javax.mail.PasswordAuthentication;
  6. import javax.mail.Session;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.InternetAddress;
  9. import javax.mail.internet.MimeMessage;
  10.  
  11. public class JavaMailApp
  12. {
  13.       public static void main(String[] args) {
  14.             Properties props = new Properties();
  15.             /** Parâmetros de conexão com servidor Gmail */
  16.             props.put("mail.smtp.host", "smtp.gmail.com");
  17.             props.put("mail.smtp.socketFactory.port", "465");
  18.             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  19.             props.put("mail.smtp.auth", "true");
  20.             props.put("mail.smtp.port", "465");
  21.  
  22.             Session session = Session.getDefaultInstance(props,
  23.                         new javax.mail.Authenticator() {
  24.                              protected PasswordAuthentication getPasswordAuthentication()
  25.                              {
  26.                                    return new PasswordAuthentication("g16srm@gmail.com", "SRM_1819");
  27.                              }
  28.                         });
  29.  
  30.             /** Ativa Debug para sessão */
  31.             session.setDebug(true);
  32.  
  33.             try {
  34.  
  35.                   Message message = new MimeMessage(session);
  36.                   message.setFrom(new InternetAddress("g16srm@gmail.com")); //Remetente
  37.  
  38.                   Address[] toUser = InternetAddress //Destinatário(s)
  39.                              .parse("g16srm@gmail.com");  
  40.  
  41.                   message.setRecipients(Message.RecipientType.TO, toUser);
  42.                   message.setSubject("Enviando email com JavaMail");//Assunto
  43.                   message.setText("Enviei este email utilizando JavaMail com minha conta GMail!");
  44.                   /**Método para enviar a mensagem criada*/
  45.                   Transport.send(message);
  46.  
  47.                   System.out.println("Feito!!!");
  48.  
  49.              } catch (MessagingException e) {
  50.                   throw new RuntimeException(e);
  51.             }
  52.       }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement