Sometimes there are situations when management people or your clients ask to send email after every test execution.
Here I am going to show you a sample Program in which we can automate the process of Sending Mail after every Test Execution in Selenium using Java Mail API. Click here to Download the Java Mail jar file.
Glossary and utilities we will be using here are as follows:
SMTP: Acronym for Simple Mail Transfer Protocol. It provides a mechanism to deliver email.
We will be using 465 as SMTP Port to send the mail.
RecipientType can be To, CC,BCC like in any other mail.We are using 'To' here.
1.) We will be creating Session with Email id and password using following code.
return new PasswordAuthentication("GmailAddress","Password");
}
}
); 2.)We will be using Multipart.
It is used by browsers and HTTP clients to upload files to the server. How data transfer take place..?Below is the sample http packet POST http://127.0.0.1/GetPostRequest.php HTTP/1.1
Note: In the above requestNotice the string "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryX6nBO7q27yQ1JNbb".
This is the part that tells the server:
1) the HTTP request is a multipart request
2) The separator between different chunks of data.
3.)Setting Subject and Message body using following code message.setSubject("Script Status"); // used for setting Subject line messageBodyPart.setText("PFA");
4.) Sending mail can be done using following snippet.
Transport.send(message); Here is the whole code used for Sending Mail using Gmail account with Attachment of TestNG Reports
No comments:
Post a Comment