Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
2.8k views
by Expert (572 points)
retagged by
I am currently using Jenkins as my CI tool. After my automation scripts are executed it sends a console log as an email to a specific set of recipients.

I want email body and the subject to be customized as per my requirements.

I am using Java as my programming language. Is there any way to override this email subject and body using Java?

2 Answers

1 like 0 dislike
by The go-to Tester (181 points)
edited by
 
Best answer

Changing email body programmatically is possible now.

  • You can go to Manage Jenkins > Configure System and set Default Content as

           ${FILE,path="email-body.html"}

           under Extended E-mail Notification area.


Now, you just have to generate email-body.html file programmatically directly under your workspace root.

Sample code:

@After
    public void writeLog() throws IOException {
        File file = new File("email-body.html");
        StringBuilder htmlBuilder = new StringBuilder();
        htmlBuilder.append("<html>");
        htmlBuilder.append("<head><title>Hello World</title></head>");
        htmlBuilder.append("<body><p>Look at my body!</p></body>");
        htmlBuilder.append("</html>");
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write(htmlBuilder.toString());
        bufferedWriter.close();

    }

Also, make sure that default content type is set to HTML.

0 like 0 dislike
by
No! Because, email subject and email content are set using an environment variable. Environment variables can not be changed throughout the process once set.

So, you can either write a custom program to send an email or you can create an attachment which will be attached to your email.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...