Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  * https://www.google.com/settings/security/lesssecureapps
  5.  */
  6.  
  7. package SendEmail;
  8.  
  9. /**
  10.  *
  11.  * @author Naveen
  12.  */
  13. import java.io.File;
  14. import java.util.*;
  15. import javax.activation.DataHandler;
  16. import javax.mail.*;
  17. import javax.mail.internet.*;
  18. import javax.mail.internet.MimeMessage;
  19. import javax.mail.util.ByteArrayDataSource;
  20. public class EmailSend {
  21.  
  22.     public static void main(String args[]){
  23.         try{
  24.             String host ="smtp.gmail.com" ;
  25.             String user = "honeywellsuporte@gmail.com";
  26.             String pass = "19honeywell19";
  27.             String to = "tarik_mouallem@hotmail.com";
  28.             String from = "honeywellsuporte@gmail.com";
  29.             String subject = "Mensagem de alerta";
  30.             String messageText = "A notificação foi feita com sucesso !\n testando\n testando";
  31.             boolean sessionDebug = false;
  32.            
  33.             Properties props = System.getProperties();
  34.  
  35.             props.put("mail.smtp.starttls.enable", "true");
  36.             props.put("mail.smtp.host", host);
  37.             props.put("mail.smtp.port", "587");
  38.             props.put("mail.smtp.auth", "true");
  39.             props.put("mail.smtp.starttls.required", "true");
  40.  
  41.             java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  42.             Session mailSession = Session.getDefaultInstance(props, null);
  43.             mailSession.setDebug(sessionDebug);
  44.             Message msg = new MimeMessage(mailSession);
  45.             msg.setFrom(new InternetAddress(from));
  46.             InternetAddress[] address = {new InternetAddress(to)};
  47.             msg.setRecipients(Message.RecipientType.TO, address);
  48.             msg.setSubject(subject); msg.setSentDate(new Date());
  49.             //msg.setText(messageText);
  50.            
  51.             Multipart multipart = new MimeMultipart();
  52.             MimeBodyPart messageBodyPart = new MimeBodyPart();
  53.             String message = messageText;
  54.             messageBodyPart.setText(message, "utf-8", "html");
  55.             multipart.addBodyPart(messageBodyPart);
  56.             MimeBodyPart attachmentBodyPart = new MimeBodyPart();
  57.             attachmentBodyPart.attachFile(new File("/home/gpesc15/Área de Trabalho/PMOC.pdf"), "application/pdf", null);
  58.             multipart.addBodyPart(attachmentBodyPart);
  59.             msg.setContent(multipart);
  60.  
  61.  
  62.            Transport transport=mailSession.getTransport("smtp");
  63.            transport.connect(host, user, pass);
  64.            transport.sendMessage(msg, msg.getAllRecipients());
  65.            transport.close();
  66.            System.out.println("message send successfully");
  67.         }catch(Exception ex)
  68.         {
  69.             System.out.println(ex);
  70.         }
  71.  
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement