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

0 like 0 dislike
259 views
by The go-to Tester (262 points)
retagged by
Hey, i am working in a project, where while submitting the task, it automatically generates a mail and send to the user.

I have to validate the mail content as well the UI , Can you please suggest me some tools are methods to automate emails?

1 Answer

0 like 0 dislike
by
selected by
 
Best answer

We usually log in via POP3 to the server and fetch an email and validate it. If its microsoft exchange, you will have to work with exchange API and validate the response.

Java code sample to fetch an email using POP3.

 
import java.util.Properties;
 
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
 
public class ValidateEmails{
 
   public static void get(String host, String storeType, String user,
      String password) 
   {
      try {
 
      //create properties field
      Properties properties = new Properties();
 
      properties.put("mail.pop3.host", host);
      properties.put("mail.pop3.port", "995");
      properties.put("mail.pop3.starttls.enable", "true");
      Session emailSession = Session.getDefaultInstance(properties);
  
      //create the POP3 store object and connect with the pop server
      Store store = emailSession.getStore("pop3s");
 
      store.connect(host, user, password);
 
      //create the folder object and open it
      Folder emailFolder = store.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);
 
      // retrieve the messages from the folder in an array and print it
      Message[] messages = emailFolder.getMessages();
      System.out.println("messages.length---" + messages.length);
 
      for (int i = 0, n = messages.length; i < n; i++) {
         Message message = messages[i];
         System.out.println("---------------------------------");
         System.out.println("Email Number " + (i + 1));
         System.out.println("Subject: " + message.getSubject());
         System.out.println("From: " + message.getFrom()[0]);
         System.out.println("Text: " + message.getContent().toString());
 
      }
 
      //close the store and folder objects
      emailFolder.close(false);
      store.close();
 
      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
 
   public static void main(String[] args) {
 
      String host = "pop.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "[email protected]";// change accordingly
      String password = "password"; // change accordingly
 
      get(host, mailStoreType, username, password);
 
   }
 
}
 
Hope that helps!


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!

...