Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package auxiliare;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- public class EmailService extends BaseLogger {
- private String host = Config.getByName("mail_host");
- private String user = Config.getByName("mail_user");
- private String password = Config.getByName("mail_password");
- private String to = Config.getByName("mail_to");
- private String reportFileLocation = Config.getByName("report_file");
- private Properties properties = new Properties();
- private DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
- private Date date = new Date();
- private String getCurrentDate(){
- String currentDate = dateFormat.format(date);
- return currentDate;
- }
- private void initProperties(){
- properties.put("mail.smtp.host", host);
- properties.put("mail.smtp.auth", true);
- }
- private Session getSession(){
- initProperties();
- Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator(){
- protected PasswordAuthentication getPasswordAuthentication(){
- return new PasswordAuthentication(user,password);
- }
- });
- return session;
- }
- private MimeMessage composeMessage(){
- MimeMessage mail = new MimeMessage(getSession());
- try {
- mail.setFrom(new InternetAddress(user));
- mail.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- mail.setSubject("Rezultate teste pentru " + getCurrentDate());
- MimeBodyPart messageBody = new MimeBodyPart();
- messageBody.setText("Atasat este raportul generat de TestNG pentru " + getCurrentDate());
- DataSource fisier = new FileDataSource(reportFileLocation);
- messageBody.setDataHandler(new DataHandler(fisier));
- messageBody.setFileName("Report" + getCurrentDate());
- MimeMultipart finalMessage = new MimeMultipart(messageBody);
- mail.setContent(finalMessage);
- }
- catch (MessagingException e){
- logger.debug("A avut loc o eroare la compunerea mailului:");
- logger.debug(e.getMessage());
- }
- return mail;
- }
- public void sendReport(){
- try{
- Transport.send(composeMessage());
- }
- catch(MessagingException e){
- logger.debug("A avut loc o eroare la trimiterea mailului:");
- logger.debug(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement