繁体   English   中英

Selenium Java Mail发送错误

[英]Selenium Java Mail sending error

我正在使用以下代码通过硒发送邮件。

 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-Additional.jar

3-Java邮件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,然后尝试发送邮件。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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