Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import javax.activation.*;
  4. import javax.mail.Message;
  5. import javax.mail.MessagingException;
  6. import javax.mail.Multipart;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeBodyPart;
  12. import javax.mail.internet.MimeMessage;
  13. import javax.mail.internet.MimeMultipart;
  14. public class EmailUtility {
  15.  
  16.     public static void main(String args[]) {
  17.        
  18.     }
  19.    
  20.         public static void SendMail(String to,String subject,String text) {
  21.  
  22.             final String username = "ITQGTestMail@gmail.com";
  23.             final String password = "benqbenq";
  24.  
  25.             Properties props = new Properties();
  26.             props.put("mail.smtp.auth", "true");
  27.             props.put("mail.smtp.starttls.enable", "true");
  28.             props.put("mail.smtp.host", "smtp.gmail.com");
  29.             props.put("mail.smtp.port", "587");
  30.  
  31.             Session session = Session.getInstance(props,
  32.               new javax.mail.Authenticator() {
  33.                 protected PasswordAuthentication getPasswordAuthentication() {
  34.                     return new PasswordAuthentication(username, password);
  35.                 }
  36.               });
  37.  
  38.             try {
  39.  
  40.                 Message message = new MimeMessage(session);
  41.                 message.setFrom(new InternetAddress("ITQGTestMail@gmail.com"));
  42.                 message.setRecipients(Message.RecipientType.TO,
  43.                     InternetAddress.parse(to));
  44.                 message.setSubject(subject);
  45.                 message.setText(text);
  46.                
  47.                
  48.                
  49.                
  50.                
  51.                 Transport.send(message);
  52.                 System.out.println("Done");
  53.                    
  54.                
  55.  
  56.             } catch (MessagingException e) {
  57.                 throw new RuntimeException(e);
  58.             }
  59.         }
  60.  
  61.        
  62.  
  63.         public static void SendMail(String to,String subject,String text, String[] attachments) {
  64.  
  65.             final String username = "ITQGTestMail@gmail.com";
  66.             final String password = "benqbenq";
  67.  
  68.             Properties props = new Properties();
  69.             props.put("mail.smtp.auth", "true");
  70.             props.put("mail.smtp.starttls.enable", "true");
  71.             props.put("mail.smtp.host", "smtp.gmail.com");
  72.             props.put("mail.smtp.port", "587");
  73.  
  74.             Session session = Session.getInstance(props,
  75.               new javax.mail.Authenticator() {
  76.                 protected PasswordAuthentication getPasswordAuthentication() {
  77.                     return new PasswordAuthentication(username, password);
  78.                 }
  79.               });
  80.  
  81.             try {
  82.  
  83.                 Message message = new MimeMessage(session);
  84.                 message.setFrom(new InternetAddress("ITQGTestMail@gmail.com"));
  85.                 message.setRecipients(Message.RecipientType.TO,
  86.                     InternetAddress.parse(to));
  87.                 message.setSubject(subject);
  88.                 message.setText(text);
  89.                
  90.            
  91.                     Multipart multipart = new MimeMultipart();
  92.                     for (String var : attachments)
  93.                     {
  94.                         try {
  95.                    
  96.                             MimeBodyPart bodyPart = new MimeBodyPart();
  97.                             DataSource source = new FileDataSource(var);
  98.                             bodyPart.setDataHandler(new DataHandler(source));
  99.                             bodyPart.setFileName(var);
  100.                             multipart.addBodyPart(bodyPart);
  101.                            
  102.                         }
  103.                         catch (Exception e) {
  104.                             System.out.println(e.getStackTrace());
  105.                         }
  106.                     }
  107.                     message.setContent(multipart);
  108.                
  109.                
  110.                
  111.                
  112.                
  113.                 Transport.send(message);
  114.                 System.out.println("Done");
  115.                    
  116.                
  117.  
  118.             } catch (MessagingException e) {
  119.                 throw new RuntimeException(e);
  120.             }
  121.         }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement