Advertisement
zubayer007

Sending Email

Sep 26th, 2017
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1.  
  2. import java.util.Properties;
  3. import javax.activation.*;
  4. import javax.mail.*;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeBodyPart;
  7. import javax.mail.internet.MimeMessage;
  8. import javax.mail.internet.MimeMultipart;
  9.  
  10. public class SendEmail {
  11.      
  12.  
  13. public void send2() {
  14.  
  15.    
  16.    
  17.    
  18.  
  19.     final String username = "Write Your Email@gmail.com"; //ur email
  20.     final String password = "Write Your Password";
  21.  
  22.     Properties props = new Properties();
  23.     props.put("mail.smtp.auth", true);
  24.     props.put("mail.smtp.starttls.enable", true);
  25.     props.put("mail.smtp.host", "smtp.gmail.com");
  26.     props.put("mail.smtp.port", "587");
  27.  
  28.     Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  29.     protected PasswordAuthentication getPasswordAuthentication() {
  30.         return new PasswordAuthentication(username, password);
  31.     }                            
  32. });
  33.  
  34.     try {
  35.        
  36.         Message message = new MimeMessage(session);
  37.         message.setFrom(new InternetAddress("Write Your Email@gmail.com"));//ur email
  38.         message.setRecipients(Message.RecipientType.TO,
  39.                 InternetAddress.parse("Write Send to Email@gmail.com"));//u will send to
  40.         message.setSubject("Subject");    
  41.         message.setText("PFA");
  42.         MimeBodyPart messageBodyPart = new MimeBodyPart();
  43.         Multipart multipart = new MimeMultipart();
  44.  
  45.  
  46.      
  47.      
  48.     //attached 1 --------------------------------------------
  49.         String file = "path of file";
  50.         String fileName = "AnyName1";
  51.     messageBodyPart = new MimeBodyPart();  
  52.     DataSource source = new FileDataSource(file);      
  53.     messageBodyPart.setDataHandler(new DataHandler(source));
  54.     messageBodyPart.setFileName(fileName);
  55.     multipart.addBodyPart(messageBodyPart);
  56.     //------------------------------------------------------    
  57.      
  58.      //attached 2 --------------------------------------------  
  59.        String file2="path of file";
  60.        String fileName2 = "AnyName2";
  61.     messageBodyPart = new MimeBodyPart();  
  62.     DataSource source2 = new FileDataSource(file2);      
  63.     messageBodyPart.setDataHandler(new DataHandler(source2));
  64.     messageBodyPart.setFileName(fileName2);
  65.     multipart.addBodyPart(messageBodyPart);
  66.   //attached 3------------------------------------------------
  67.        
  68.        String file3="path of file";
  69.        String fileName3 = "AnyName3";
  70.     messageBodyPart = new MimeBodyPart();  
  71.     DataSource source3 = new FileDataSource(file3);      
  72.     messageBodyPart.setDataHandler(new DataHandler(source3));
  73.     messageBodyPart.setFileName(fileName3);
  74.     multipart.addBodyPart(messageBodyPart);
  75.     //attached 4------------------------------------------------
  76.     String file4="path of file";
  77.        String fileName4 = "AnyName4";
  78.     messageBodyPart = new MimeBodyPart();  
  79.     DataSource source4 = new FileDataSource(file4);      
  80.     messageBodyPart.setDataHandler(new DataHandler(source4));
  81.     messageBodyPart.setFileName(fileName4);
  82.     multipart.addBodyPart(messageBodyPart);
  83.     //>>>>>>>>
  84.    
  85.    
  86.    
  87.         message.setContent(multipart);
  88.  
  89.        
  90.         System.out.println("sending");
  91.         Transport.send(message);
  92.         System.out.println("Done");
  93.        
  94.    
  95.  
  96.            
  97. }catch (MessagingException e) {
  98.         e.printStackTrace();
  99.     }
  100.   }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement