java - Selenium Java 邮件发送错误

标签 java selenium selenium-webdriver jakarta-mail

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

但是我收到以下错误。预期是缺少一些文件 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

请给出解决方案。

最佳答案

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

1 - Activation.jar

2 - 附加.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,然后尝试发送邮件。

关于java - Selenium Java 邮件发送错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31646612/

相关文章:

python - 过滤掉 'display:none' 作为标记属性或在其 CSS 中的 HTML 元素

java - Vaadin 7 示例与 Maven

java - 我在卸载 Java 8 (MAC) 时遇到问题

c# - Selenium:将 chromeDriver 嵌入到一个 exe 中

python - 为什么 split is_text_present() 会导致 Firefox 间歇性出现 StaleElementReferenceException(但 Chrome 或 phantomjs 不会)?

Selenium - LoadableComponent 和 SlowLoadableComponent 有什么区别

java - 在测试中哪里使用ajax方法?如何查看我可以在哪里使用它们?

java - 按下时停止 JButton 突出显示

java - 使用 byte、short 和 char 毫无意义

javascript - Selenium 可以用于测试单页 JavaScript 应用程序吗?