如何解决异常"java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger"。我正在尝试通过Selenium网络驱动程序发送电子邮件



这是我发送电子邮件的代码

Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.abc.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            Session session = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication("vijaya.teke@abc.com", "********");
                        }
                    });
            try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("vijaya.teke@abc.com"));
                message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("vijaya.teke@abc.com"));
                message.setSubject("Testing Subject");
                BodyPart messageBodyPart1 = new MimeBodyPart();
                messageBodyPart1.setText("This is message body");
                MimeBodyPart messageBodyPart2 = new MimeBodyPart();
                String filename = "D:\Automation\Automation workspace\TestSelenium\Screenshots";
                DataSource source = new FileDataSource(filename);
                messageBodyPart2.setDataHandler(new DataHandler(source));
                messageBodyPart2.setFileName(filename);
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(messageBodyPart2);
                multipart.addBodyPart(messageBodyPart1);
                message.setContent(multipart);
                Transport.send(message);
                System.out.println("=====Email Sent=====");
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }

我正在遇到错误"。

下面对我有用:

package Reporting;
/**
 * @author Shubham Jain
 *
 */
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import Utilities.Configuration;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * Note : This Class zip the folder of reports and sent the email via gmail. Make sure files do not contains any JS files or extention banned by gmail. 
 * Don't use automation log in this class, It will override all before logs of the same session
 * */
public class SendMailClass 
{
    public SendMailClass(){
    }
/*  static ExcelLib xl = new ExcelLib();
    static String browserType = xl.getXLcellValue("Controller", 1, 0);
    static String serverName = xl.getXLcellValue("Controller", 1, 1);*/
    private static String OSNAMES = System.getProperty("os.name").toLowerCase();
    static String OS = OSNAMES.split(" ")[0];
    static Date currentDate = new Date();
    static SimpleDateFormat dateFormatForFileName = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//dd/MM/yyyy
    static String fileDateFormet = dateFormatForFileName.format(currentDate);
//  static String testoutputfolderpath = System.getProperty("user.dir")+ File.separator +"target"+ File.separator+ "cucumber-html-reports";
//  static String zipFilefolderpath = System.getProperty("user.dir")+ File.separator +"reports"+File.separator+"results"+fileDateFormet;  // Zip Reports Files
    static String zipLogFilefolderpath = System.getProperty("user.dir")+ File.separator +"logs"+File.separator+"logs.txt";
/*  static File folder = new File(zipLogFilefolderpath);
    static File zipFile = new File(zipFilefolderpath);*/
    static String subject = "Shubham Automation Application Test Cucumber report";
    static String PlatformName = Configuration.getConfigurationValueForProperty("Report-Upload-Platform-Name");
    static String emailForsendingnotification = Configuration.getConfigurationValueForProperty("Email-Sending-Notification");
    static String PasswordForsendingnotification = Configuration.getConfigurationValueForProperty("Pass-Sending-Notification");
    public static void main(String[] args) throws IOException {
        try {
            execute();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    public static void execute() throws Exception
    {
    //  ExcelLib xl = new ExcelLib();

    String FilenameLocalPath=ZipReports.zipFilePathName();
    String[] b = FilenameLocalPath.split("\\");
    String FileName =  b[b.length-1];
    String text = "Hi Greetings,n This is Final Automation Test Report.n This Auto-generated report please do not reply.n Find the reports on "+PlatformName+" account, The Report file name is : "+FileName+".n Please download your file from given URL: https://www.dropbox.com/home/Apps/AutomationResults/Apps/ShubhamCucumberReports";
        //////////////////////////////////////////////////////////////////////////////////
        ////////////////////////Create a zip file of test-output folder /////////////////
    //    path of file which contains report
  //      String path = zipFilefolderpath;
        String path2 = zipLogFilefolderpath;
     //   No of recipients of the report, you can have many separated by comma
        String mailto = "shubham.jain@abc.co.in";
        String mailcc = "shubham.jain@abc.co.in";
        String mailbcc = "shubham.jain@abc.co.in";

        String[] to={mailto};
        String[] cc={mailcc};
        String[] bcc={mailbcc};

      //  email user name and password of sender 

        SendMailClass.sendMail(emailForsendingnotification,   
                            PasswordForsendingnotification,                     
                            "smtp.gmail.com",
                            "465",
                            "true",
                            "true",
                            "javax.net.ssl.SSLSocketFactory",
                            "false",
                             to,
                             cc,
                             bcc,
                            subject,
                            text,
                            path2,
                            "Test-Report");
      }
      public  static boolean sendMail(String userName,
                String passWord,
                String host,
                String port,
                String starttls,
                String auth,
                String socketFactoryClass,
                String fallback,
                String[] to,
                String[] cc,
                String[] bcc,
                String subject,
                String text,
                String attachmentPath,
                String attachmentName){
        //Object Instantiation of a properties file.
        Properties props = new Properties();
        props.put("mail.smtp.user", userName);
        props.put("mail.smtp.host", host);
        if(!"".equals(port)){
        props.put("mail.smtp.port", port);
        }
        if(!"".equals(starttls)){
            props.put("mail.smtp.starttls.enable",starttls);
            props.put("mail.smtp.auth", auth);
        }
        if(!"".equals(port)){
            props.put("mail.smtp.socketFactory.port", port);
        }
        if(!"".equals(socketFactoryClass)){
            props.put("mail.smtp.socketFactory.class",socketFactoryClass);
        }
        if(!"".equals(fallback)){
            props.put("mail.smtp.socketFactory.fallback", fallback);
        }
        try{
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage msg = new MimeMessage(session);
            msg.setText(text);
            msg.setSubject(subject);
            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            //////////////////////////////////
            BodyPart objMessageBodyPart = new MimeBodyPart();
            objMessageBodyPart.setText(text);
            /////////////////////////////////
            messageBodyPart.setText(text);
            messageBodyPart.setContent(text, "text/html");
            DataSource source = new FileDataSource(attachmentPath);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("Logs.txt");
            multipart.addBodyPart(messageBodyPart);
            multipart.addBodyPart(objMessageBodyPart);
/*
            /////////////////////////////// Add another File ////////////////
            DataSource source2 = new FileDataSource(attachmentPath2);  
            BodyPart messageBodyPart2 = new MimeBodyPart();           
            messageBodyPart2.setText(text);
            messageBodyPart2.setDataHandler(new DataHandler(source2));
            messageBodyPart2.setFileName(attachmentPath2);
            messageBodyPart2.setFileName("Reports.txt");
            multipart.addBodyPart(messageBodyPart2);
            /////////////////////////////////////////////////////////////////////
            */
            msg.setContent(multipart);
            msg.setFrom(new InternetAddress(userName));
            for(int i=0;i<to.length;i++){
                msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(to[i]));
            }
            for(int i=0;i<cc.length;i++){
                msg.addRecipient(Message.RecipientType.CC, new
InternetAddress(cc[i]));
            }
            for(int i=0;i<bcc.length;i++){
                msg.addRecipient(Message.RecipientType.BCC, new
InternetAddress(bcc[i]));
            }
            msg.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(host, userName, passWord);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
            //closing .exe of chrome or phantom driver binaries
        //  AppDriver.clearBrowserContext(Page.driver);
            return true;
        } catch (Exception mex){
            mex.printStackTrace();
            return false;
        }
    }
}

删除一些与您无关的代码。像zip调用功能等

最新更新