Join 500+ Software Testing professionals. Solve problem and get your problem solved. Register Now!

Is there any way to modify email subject and the body in Jenkins using my automated test script written in Java?

Solved 2 Answers 1110 Views
0 2
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

Best answer

Changing email body programmatically is possible now.

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


           under Extended E-mail Notification area.

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

Sample code:

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


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

answered by (151 points)
edited 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.
answered by

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.