Search code examples
google-apijmetersmtpgmail

JMeter: how to send formatted text via SMPT sampler


Is there a way to send a formatted text with links via JMeter SMTP sampler?

Scenario: I need to send an email, where one of the words e.g. My "Instagram" will have a link to my instagram page.

Option 1: Create such email in Gmail, send it to myself, then download it as .eml file and send use "Send .eml" option in SMTP sampler.

However, my issue is that these links should be changed and lead to different instagram pages with each new email sent, thus I need to pass it as a variable from CSV file. This seems to be impossible to achieve with .eml file as it needs to be modified before each request. Unless there is a way?

Option 2 (preferred): Somehow I need to format text in "Message body" of SMTP sampler. I've tried to copy/paste the same style and tags from "Original" .eml file, but it is always sent as a plain text and Gmail won't format it on client side.

Here is an example of RAW Gmail text with formatted link which I've tried to use in "Message" text box of SMTP Sampler:

Visit my account @dummyaccount<https://l.instagram.com/?u=https%3A%2F%2Ftaplink.> for more info.

Expecting to see the following in the email: Visit my account @dummyaccount - where @dummyaccount is a hyperlink

Actual: Visit my account @dummyaccounthttps://l.instagram.com/?u=https%3A%2F%2Ftaplink. for more info.

Any suggestion will be greatly appreciated.


Solution

  • I don't think you can customize the SMTP Sampler messages this way. You will need to either compose the message in an email application, save it to .eml file and send this .eml or if you want to build the message content dynamically you can use JSR223 Sampler and Groovy language for sending your message.

    Example code:

    Properties props = new Properties();
    props.put("mail.smtp.host", "your-smtp-server-here");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    
    def session = javax.mail.Session.getInstance(props, new CredentialsAuthentication() as javax.mail.Authenticator)
    def msg = new javax.mail.internet.MimeMessage(session)
    
    def from = new javax.mail.internet.InternetAddress("your-email-address-here", "your first and last name");
    msg.setFrom(from);
    
    def toAddress = new javax.mail.internet.InternetAddress("your-recipient-here ");
    
    msg.setRecipient(javax.mail.Message.RecipientType.TO, toAddress);
    
    msg.setSubject("Test");
    msg.setContent("<html>\n" +
            "<body>\n" +
            "\n" +
            "<a href=\"http://link-to-your-instagram-profile\">\n" +
            "Link to my Instagram</a>\n" +
            "\n" +
            "</body>\n" +
            "</html>", "text/html")
    javax.mail.Transport.send(msg);
    
    
    class CredentialsAuthentication extends javax.mail.Authenticator {
    
        @Override
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
            return new javax.mail.PasswordAuthentication("your-username@most-probably-with-email.com", "your-password-here")
        }
    }