0

Below is the java code which is used in an email with an excel attachment, but this excel file is getting from another class. while running this send email class in eclipse mail sent successfully but when I run through the batch file getting an error as cannot find the symbol in the lone "String difference_path =Difference_directory.getLatestFilePath();".

public class Send_email{
static String status=null;
public static Properties CONFIG = null;

public static void main(String[] args) throws Exception {
      
    String difference_path = Difference_directory.getLatestFilePath();
    System.out.println(difference_path);
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.port", 587); 
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    
    //Loading Config file
Properties CONFIG = new Properties();
FileInputStream fs = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resource\\config2.properties");

    CONFIG.load(fs);
    String FROM= CONFIG.getProperty("FROM");
    String FROMNAME= CONFIG.getProperty("FROMNAME");
    String SMTP_USERNAME= CONFIG.getProperty("SMTP_USERNAME");
    String SMTP_PASSWORD= CONFIG.getProperty("SMTP_PASSWORD");
    String HOST= CONFIG.getProperty("HOST");
    String TO="";
    
    // folder path for execution report
     
    final String FOLDER_PATH = "C:\\Users\\rramesh\\eclipse-workspace\\Financial_API\\test-output";
    getStatus(FOLDER_PATH + "/testng-results.xml");
    
    
    if(status.equals("PASSED")){
        TO=CONFIG.getProperty("PassMail");
    }else if(status.equals("FAILED")){
        TO=CONFIG.getProperty("FailMail");
    }
    
       Session session = Session.getDefaultInstance(props);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(FROM,FROMNAME));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(TO));
        message.setSubject(status + " : Financial Block" );
        BodyPart messageBodyPart = new MimeBodyPart();
        String myMessage1="Hi All,";
        String myMessage2="<br>Data is Mismatchng between Database Search and Elastic Search";
        String myMessage3="<br>Regards<br>QA Automation Team";
        //messageBodyPart.setText(myMessage1);
        messageBodyPart.setContent(myMessage1 + "<br>"+ myMessage2 + "<br>"+ myMessage3, "text/html");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        messageBodyPart = new MimeBodyPart();
        //String filename = FOLDER_PATH + "/eTicket-Automation-report.html";
        String efile = FOLDER_PATH + "/emailable-report.html";
        DataSource src = new FileDataSource(difference_path);
        messageBodyPart.setDataHandler(new DataHandler(src));
        //DataSource source = new FileDataSource(filename);
        //messageBodyPart.setDataHandler(new DataHandler(source));
        String reportName="";
        
            reportName="Financial Block automation";
        
        messageBodyPart.setFileName(reportName + ".xlsx");
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
         Transport transport = session.getTransport();
         
         try
            {
                System.out.println("Sending...");
                
                
                // Connect to Amazon SES using the SMTP username and password you specified above.
                transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
                 
                // Send the email.
                transport.sendMessage(message, message.getAllRecipients());
                
                System.out.println("Email sent!");
            }
            catch (Exception ex) {
                System.out.println("The email was not sent.");
                System.out.println("Error message: " + ex.getMessage());
            }
            finally
            {
                // Close and terminate the connection.
                transport.close();
            }
    
}

public static void getStatus(String filePath) throws SAXException, IOException, ParserConfigurationException {
    File inputFile = new File(filePath);
    DocumentBuilderFactory dbFactory 
    = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputFile);
    doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("testng-results");
    Node nNode = nList.item(0);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        int failed = Integer.parseInt(eElement.getAttribute("failed"));
        int skipped = Integer.parseInt(eElement.getAttribute("skipped"));

        if(failed!=0 || skipped!=0)
            status="FAILED";
        else
            status="PASSED";
    }
}

}

And the batch file is cd
cd C:\Users\rramesh\eclipse-workspace\Financial_API\src\main\java\Utlities javac -cp C:\JarLib*;. Send_email.java cd.. java -cp C:\JarLib*;. Utlities.Send_email

ramesh
  • 15
  • 5
  • Take care of java naming conventions. variable names should start with lower case character and all names should be camelCase not snake_case. And also you should indent your code properbly. Your code is very hard to read. – Jens Feb 14 '23 at 15:09

0 Answers0