上课后无法发送电子邮件

时间:2018-09-11 10:18:57

标签: testng html-email

我无法附加最新的html报告并通过电子邮件发送。     我有3节课:     第一个-基础类,分为两部分,第一部分是我打开网站的地方,第二部分是退出浏览器并调用电子邮件报告类。     2-测试脚本-我正在运行的实际脚本     第三-电子邮件报告,因此在这里我将发送带有在test-output文件夹中生成的HTML报告的邮件。

    public abstract class BaseClass{

    public static WebDriver driver;

    @BeforeClass
    public static void TomtomSupport()
    {
        ChromeOptions chrome = new ChromeOptions();
        //chrome.addArguments("--headless");
        chrome.addArguments("disable-infobar");

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Gaurn\\Desktop\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver(chrome);
        driver.manage().window().maximize();


        driver.navigate().to("http://uk.support.tomtom.com/");
        System.out.println("Step 1: Open tomtom support website.");
        /*driver.findElement(By.xpath("//a[contains(text(),'Continue In UK')]")).click();
        System.out.println("Step 2: Clicked on Continue in UK of country pop up");*/
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        //Accept cookies
        driver.findElement(By.xpath("//button[contains(text(),'Accept')]")).click();
        System.out.println("Step 3: Clicked on Accept");

        /*
        //Decline Cookies
        driver.findElement(By.xpath("//span[@class='tt-cookie-bar-nav']/a")).click();
        System.out.println("Step 3: Clicked on Decline");
    */}

    @AfterClass
    public void teardown() throws InterruptedException, IOException 
    {
        try {
            driver.quit();
            System.out.println("Last Step: Browser Closed");
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        finally{
            EmailReport.sendingEmail();
        }
    }

}

2nd is test script :



    public class SupportHome extends BaseClass{
@SuppressWarnings("unused")
@Test
  public void SelectProduct() {
      HeaderSelection.headerSelect();
}
}




3rd is email report

    public class EmailReport {
    public static void sendingEmail() throws InterruptedException, IOException{
        String USER_NAME = "abc@outlook.com";  
        String PASSWORD = "xyz";
        String RECIPIENT = "abc@outlook.com";
        String host = "smtp.office365.com";

        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host","host");
        props.put("smtp.office365.com", host);
        props.put("mail.smtp.user",USER_NAME);
        props.put("mail.smtp.password",PASSWORD);
        props.put("mail.smtp.port","587");
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.ssl.trust","smtp.office365.com");
        props.put("transport_email_use_ssl","false");
        props.put("transport_email_use_tls","true");
        props.put("java.net.preferIPv4Stack","true"); 

        // Session session = Session.getDefaultInstance(props);
        Session session = Session.getInstance(props, new javax.mail.Authenticator()
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(USER_NAME, PASSWORD);
            }
        });

        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(USER_NAME));

            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(RECIPIENT));

            // Set Subject: header field
            message.setSubject("This is the Subject Line!");


            // Create the message part 
            BodyPart messageBodyPart = new MimeBodyPart();

            // Fill the message
            messageBodyPart.setText("This is message body");

            // Create a multi-part message
            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);
            boolean isFileExists = false;
            File file1 = new File("C:\\Users\\Gaurn\\Desktop\\ccwebqa-master@03c7378be40\\test-output\\emailable-report.html");
            for(int i=0; i<=30; i++) {
                if(file1.exists()) {
                    isFileExists = true;
                    break;
                }
                else {
                    Thread.sleep(1000*2);
                }
            }

            if(isFileExists) {
                // Part two is attachment
                messageBodyPart = new MimeBodyPart();
                String filename = "C:\\Users\\Gaurn\\Desktop\\ccwebqa-master@03c7378be40\\test-output\\emailable-report.html";
                //  String filename = "C:\\Users\\Gaurn\\Pictures\\Agent Selecting manually.png";
                DataSource source = new FileDataSource(filename);
                messageBodyPart.setDataHandler(new DataHandler(source));
                messageBodyPart.setFileName(filename);
                multipart.addBodyPart(messageBodyPart);

                // Send the complete message parts
                message.setContent(multipart );

                // Send message
                Transport transport = session.getTransport("smtp");
                System.out.println("message started");
                transport.connect(host, USER_NAME, PASSWORD);
                System.out.println("message recieved");
                transport.sendMessage(message, message.getAllRecipients());
                System.out.println("Sent message successfully....");
                transport.close();

                //Delete test-output folder after execution
                FileUtils.deleteDirectory(new File("C:\\Users\\Gaurn\\Desktop\\ccwebqa-master@03c7378be40\\test-output"));
            }
        }
        catch (MessagingException msgex)
        {
            msgex.printStackTrace();
        }
    }
}

0 个答案:

没有答案