Handling Emails in Selenium



  • Email code is required for anything when we want to send someone our content be it our test case results or anything of the sorts.

  • This can be integrated into a hybrid framework as well.

  • The JavaMail is an API that is used to compose,write and read electronic messages(emails).

  • The JavaMail API provides protocol-independent and platform-independent framework for sending and receiving mails.

  • The javax.mail and javax.mail.activation packages contain the core classes of the JavaMail API.

  • The JavaMail facility can be applied to many events. It can be used at the time of registering the user (sending notification, such as thanks for your interest to my site), forgot password (sending a password to the user’s email id), sending notifications for important updates etc. So there can be various usage of the Java mail API.

  • For sending the email using JavaMail API, you need to load the two jar files:

    • mail.jar
    • activation.jar

Sending email with attachment using JavaMail API:

There are total 7 steps for sending attachments with email. They are:

  1. Get the session object.

  2. Compose a message.

  3. Create ‘MimeBodyPart’ object and set your message text.

  4. Create new ‘MimeBodyPart’ object and set a ‘DataHandler’ object to this object.

  5. Create ‘Multipart’ object and add ‘MimeBodyPart’ objects to this object.

  6. Set the ‘multipart’ object to the message object.

  7. Send message.