Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package auxiliare;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Properties;
  7.  
  8. import javax.activation.DataHandler;
  9. import javax.activation.DataSource;
  10. import javax.activation.FileDataSource;
  11. import javax.mail.Message;
  12. import javax.mail.MessagingException;
  13. import javax.mail.PasswordAuthentication;
  14. import javax.mail.Session;
  15. import javax.mail.Transport;
  16. import javax.mail.internet.InternetAddress;
  17. import javax.mail.internet.MimeBodyPart;
  18. import javax.mail.internet.MimeMessage;
  19. import javax.mail.internet.MimeMultipart;
  20.  
  21. public class EmailService extends BaseLogger {
  22.  
  23.     private String host = Config.getByName("mail_host");
  24.     private String user = Config.getByName("mail_user");
  25.     private String password = Config.getByName("mail_password");
  26.     private String to = Config.getByName("mail_to");
  27.     private String reportFileLocation = Config.getByName("report_file");
  28.     private Properties properties = new Properties();
  29.     private DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  30.     private Date date = new Date();
  31.    
  32.     private String getCurrentDate(){
  33.         String currentDate = dateFormat.format(date);
  34.         return currentDate;
  35.     }
  36.    
  37.     private void initProperties(){
  38.         properties.put("mail.smtp.host", host);
  39.         properties.put("mail.smtp.auth", true);
  40.     }
  41.    
  42.     private Session getSession(){
  43.         initProperties();
  44.         Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator(){
  45.             protected PasswordAuthentication getPasswordAuthentication(){
  46.                 return new PasswordAuthentication(user,password);
  47.             }
  48.         });
  49.         return session;
  50.     }
  51.  
  52.    
  53.     private MimeMessage composeMessage(){
  54.         MimeMessage mail = new MimeMessage(getSession());
  55.         try {
  56.            
  57.             mail.setFrom(new InternetAddress(user));
  58.             mail.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  59.             mail.setSubject("Rezultate teste pentru " + getCurrentDate());
  60.        
  61.             MimeBodyPart messageBody = new MimeBodyPart();
  62.             messageBody.setText("Atasat este raportul generat de TestNG pentru " + getCurrentDate());
  63.             DataSource fisier = new FileDataSource(reportFileLocation);
  64.             messageBody.setDataHandler(new DataHandler(fisier));
  65.             messageBody.setFileName("Report" + getCurrentDate());
  66.            
  67.             MimeMultipart finalMessage = new MimeMultipart(messageBody);
  68.             mail.setContent(finalMessage);
  69.         }
  70.         catch (MessagingException e){
  71.             logger.debug("A avut loc o eroare la compunerea mailului:");
  72.             logger.debug(e.getMessage());
  73.         }
  74.         return mail;
  75.     }
  76.    
  77.     public void sendReport(){
  78.         try{
  79.             Transport.send(composeMessage());
  80.         }
  81.         catch(MessagingException e){
  82.             logger.debug("A avut loc o eroare la trimiterea mailului:");
  83.             logger.debug(e.getMessage());
  84.            
  85.         }
  86.        
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement