简体   繁体   中英

Selenium Java Mail sending error

I am using the following code to send a mail through selenium.

 Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true"); 
        props.put("mail.debug", "true");
        props.put("mail.store.protocol", "pop3");
        props.put("mail.transport.protocol", "smtp");
             props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", "****@gmail.com");
        props.put("mail.smtp.password", "*******");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.EnableSSL.enable","true");
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
        props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
        //props.setProperty("mail.smtp.port", "465"); 
        props.setProperty("mail.smtp.socketFactory.port", "587"); 
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        try {
        //Set from address
        message.setFrom(new InternetAddress("mailchecker"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("******@gmail.com"));
        //Set subject
        message.setSubject("Test Execution Status");
        message.setText("\n"+passeddata+"\n"+faildata);
        BodyPart objMessageBodyPart = new MimeBodyPart();
        objMessageBodyPart.setText("Please Find The Attached Report File!");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(objMessageBodyPart);
        objMessageBodyPart = new MimeBodyPart();
        //Set path to the pdf report file
        String filename = System.getProperty("C:\\ITextTest3.pdf");
        //Create data source to attach the file in mail
        DataSource source = new FileDataSource(filename);
        objMessageBodyPart.setDataHandler(new DataHandler(source));
        objMessageBodyPart.setFileName(filename);
        multipart.addBodyPart(objMessageBodyPart);
        message.setContent(multipart);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, "*****@gmail.com", "*****");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();}
        finally{}

But i am getting the following error. The Expection is that it is missing some file javamail.address.map. But the mail debug information is saying file is successfully loaded as given in the below snippet.

DEBUG: URL jar:file:/C:/Users/191/Desktop/Datacede/javamail-smtp-1.4.2.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/C:/Users/191/Desktop/Datacede/javamail-smtp-1.4.2.jar!/META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre7\lib\javamail.address.map (The system cannot find the file specified)

[TestNG] Reporter com.sel.package classname@1bf3519 failed

Kindly give a solution.

Make sure you have added following JARs to your project :

1 - Activation.jar

2 - Additional.jar

3 - java-mail-1.4.4

4 - javamail-connector-4.0

5 - mail.jar

6 - pop3.jar

7 - smtp-1.4.2.jar

Following code working for me :

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail{

  public static void main(String[] args) {

final String username = "username@gmail.com";
final String password = "password";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
  });

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("from-email@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("to-email@gmail.com"));
    message.setSubject("Testing Subject");
    message.setText("Dear Mail Crawler,"
        + "\n\n No spam to my email, please!");

    MimeBodyPart messageBodyPart2 = new MimeBodyPart();  

    String filename = "Your attachment file path"
    DataSource source = new FileDataSource(filename);  
    messageBodyPart2.setDataHandler(new DataHandler(source));  
    messageBodyPart2.setFileName(filename);  



    Multipart multipart = new MimeMultipart();  
    multipart.addBodyPart(messageBodyPart1);  
    multipart.addBodyPart(messageBodyPart2);  

     message.setContent(multipart );  


    Transport.send(message);

    System.out.println("Done");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}
}

I was getting same error like yours so I have added missing JARs and it works. First add missing JARs and then try to send mail.

For Attachments you may use this code.

      //3) create MimeBodyPart object and set your message text     
      BodyPart messageBodyPart1 = new MimeBodyPart();  
      messageBodyPart1.setText("This is message body");  

      //4) create new MimeBodyPart object and set DataHandler object to this object      
      MimeBodyPart messageBodyPart2 = new MimeBodyPart();  

      String filename = "C:\\ITextTest3.pdf";//change accordingly  
      DataSource source = new FileDataSource(filename);  
      messageBodyPart2.setDataHandler(new DataHandler(source));  
      messageBodyPart2.setFileName(filename);  


      //5) create Multipart object and add MimeBodyPart objects to this object      
      Multipart multipart = new MimeMultipart();  
      multipart.addBodyPart(messageBodyPart1);  
      multipart.addBodyPart(messageBodyPart2);  

      //6) set the multiplart object to the message object  
      message.setContent(multipart );  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM