2016-10-07 2 views
2

Я использую нижеприведенный фрагмент для отправки emailable-report.html теста через почту.Как отправить тестовый отчет TestNG на электронную почту от maven?

общественного класса SampleSendMail {

public void sendmailfun() { 

    String username = "mailid"; 
    String password = "password"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", true); 
    props.put("mail.smtp.starttls.enable", true); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    Session session = Session.getInstance(props, new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(username, password); 
     } 
    }); 

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("sendingmailid")); 
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("receivingmailid")); 
     message.setSubject("Testing Subject"); 
     message.setText("PFA"); 

     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     Multipart multipart = new MimeMultipart(); 

     messageBodyPart = new MimeBodyPart(); 
     String file = "/Users/Documents/workspace/sampleproject/test-output/emailable-report.html"; 
     String fileName = "emailable-report.html"; 
     DataSource source = new FileDataSource(file); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(fileName); 
     multipart.addBodyPart(messageBodyPart); 

     message.setContent(multipart); 

     System.out.println("Sending"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } 
} 

}

В @AfterSuite, я callig эту функцию.

public void appstop() throws IOException { 
     sendingemail.sendmailfun(); 
    } 

Я получаю следующее сообщение об ошибке.

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first 

Может ли кто-нибудь помочь мне исправить это?

ответ

0
import java.io.File; 
import java.io.IOException; 
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

import org.testng.annotations.Test; 

public class SendAttachment{ 
@Test 
public static void sendmail() throws AddressException, MessagingException, InterruptedException{ 
Thread.sleep(50000); 
    System.out.println("Test mail"); 
    String[] to={"mail address","mail address"}; 
    String to2="mail address";//change accordingly 
    final String user="mail address";//change accordingly 
    final String password="password";//change accordingly 

    //1) get the session object  
    Properties properties = System.getProperties(); 
    properties.setProperty("mail.smtp.host", "smtp.gmail.com"); 
    properties.put("mail.smtp.port", "587"); //TLS Port 
    properties.put("mail.smtp.auth", "true"); //enable authentication 
    properties.put("mail.smtp.starttls.enable", "true"); 

    Session session = Session.getDefaultInstance(properties, 
    new javax.mail.Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(user,password); 
    } 
    }); 

    //2) compose message  

    MimeMessage message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(user)); 
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mailid1,mailid2")); 
    message.setSubject("ECM Regression Test suite Results"); 

    //3) create MimeBodyPart object and set your message text  
    BodyPart messageBodyPart1 = new MimeBodyPart(); 
    messageBodyPart1.setText("Please find the Regression Result in the attachment"); 

    //4) create new MimeBodyPart object and set DataHandler object to this object  
    MimeBodyPart messageBodyPart2 = new MimeBodyPart(); MimeBodyPart messageBodyPart3 = new MimeBodyPart(); MimeBodyPart messageBodyPart4 = new MimeBodyPart(); MimeBodyPart messageBodyPart5 = new MimeBodyPart(); 

    File f3=new File("D:\\svn\\CI_1.0\\seleniumScriptsRegression\\seleniumScriptsRegression\\test-output\\emailable-report.html"); 
    DataSource source4 = new FileDataSource(f3); 
    messageBodyPart5.setDataHandler(new DataHandler(source4));  
    messageBodyPart5.setFileName(f3.getName()); 


    //5) create Multipart object and add MimeBodyPart objects to this object  
    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart1); 
    multipart.addBodyPart(messageBodyPart5); 

    //6) set the multiplart object to the message object 
    message.setContent(multipart); 

    //7) send message 
    Transport.send(message); 

    System.out.println("message sent...."); 
    } 
    //} 
} 

Попытайтесь использовать как это, его работая для меня. Но когда мы запускаем этот старый отчет, доступный по электронной почте, только прикрепляемся по почте.

+0

Забота о разработке ** почему ** это сработало для вас? – RamenChef

+0

, пожалуйста, прочитайте код, который я рассказал в деталях кода в 1,2,3,4,5,6 и 7. Я вызываю этот класс в файле testng.xml с отдельным тестом, как показано ниже. –

+0

<тест имя = "Отправить"> \t \t <имя класса = "<имя пакета> .SendAttachment" /> \t

Смежные вопросы