通过电子邮件发送报告

时间:2019-03-16 10:09:47

标签: java selenium

我已经创建了硒Java类。它通过电子邮件发送Excel报告,并在线程“ main” java.lang.Error中将错误显示为Exception。错误:未解决的编译问题:smtpHost无法解析为变量

package com.rasvek.SendReportToEmail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.net.smtp.SMTP;

public class SendMailSSLWithAttachment 
{
     public static void main(String[] args) throws MessagingException
     {  
         //create object of property file
         Properties props= new Properties();
             props.put("mail.smtp.host" , "smtp.gmail.com");
         props.put("mail.smtp.socketFactory.port", "465");
         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocktFactory");
         props.put("mail.smtp.auth", "true");
         props.put("mail.smtp.port", "465");

         ///This will handle the complete authentication


         Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

             @Override
                public PasswordAuthentication getPasswordAuthentication() {
                 String username = "username";
                 String password = "password";
                    if ((username != null) && (username.length() > 0) && (password != null) 
                      && (password.length   () > 0)) {

                        return new PasswordAuthentication(username, password);
                    }

                    return null;
                }

         }); 

         try {
            //create object of mimeMessage class
             Message message = new MimeMessage(session);

             //set the From address
             message.setFrom(new InternetAddress("username"));

             //set the recipient address
             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("username"));

             //Add the subject Link
             message.setSubject("Testing Subject");

             //Create object to add multimedia type content
             BodyPart messageBodyPart1 = new MimeBodyPart();

             //set the body of email
             messageBodyPart1.setText("This is Message Body");

             //create another object to add another content
             MimeBodyPart messageBodyPart2 = new MimeBodyPart();

             //Mention the file which you want to send
             String fileName = "G:\\a.xlsx";

             //create data source and pass the file name
             DataSource source = new FileDataSource(fileName);

             //set the handler
             messageBodyPart2.setDataHandler(new DataHandler(source));

             //set the file
             messageBodyPart2.setFileName(fileName);

             //create Object of MimeMultiPart class
             Multipart multipart = new MimeMultipart();

             //add body part 1
             multipart.addBodyPart(messageBodyPart2);

             //add body part 2
             multipart.addBodyPart(messageBodyPart1);

             //set the content 
             message.setContent(multipart);

             Transport transport=session.getTransport("smtp");


            transport.connect(smtpHost, 465, "username", "password"); 
             //finally send the email
             Transport.send(message);

             System.out.println("================Email Sent==================");

        } catch (Exception e) 
         {
            e.printStackTrace();
        }

    }
}

我已经创建了我的第一个Selenium Java类,以将Excel报告从我的电子邮件发送到另一个电子邮件。错误显示在传输类中,而transport.send(message);和“线程” java.lang.Error中的异常:无法解决的编译问题:     smtpHost无法解析为变量。请以代码帮助我

0 个答案:

没有答案
相关问题