Selenium Java Mail发送错误

时间:2015-07-27 06:51:59

标签: java selenium selenium-webdriver javamail

我使用以下代码通过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{}

但是我收到以下错误。 Expection是缺少一些文件javamail.address.map。但邮件调试信息表明文件已成功加载,如下面的代码段所示。

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

请提供解决方案。

2 个答案:

答案 0 :(得分:2)

确保您已将以下JAR添加到项目中:

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

以下代码为我工作:

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);
}
}

我得到了和你一样的错误,所以我添加了丢失的JAR并且它有效。首先添加缺少的JAR,然后尝试发送邮件。

答案 1 :(得分:0)

对于附件,您可以使用此代码。

      //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 );