How to access variable from another class

Viewed 42

I'm new to java, and i cant figure how to access a variable from another class

I am trying to write a code that sends a mail with attachment which is not stored locally.And i want to access a variable written in ExcelFile Class in SendMail Class

public class ExcelFile {
   public static void generateExcelFile(String fileName,Map<int, String> studentRecord){
         XSSFWorkBook workbook = new XSSFWorkbook();
         XSSFSheet sheet= workbook.createSheet();
         writeHeaderLines(sheet);//method that writes the header lines for the excel file
         writeDataLines(studentRecord,sheet);//method that writes the data into the excel file
         ByteArrayOutputStream bos=new ByteArrayOutputStream();
         try{
             workbook.write(bos);}
         finally{
             bos.close();
         }
         byte[]excelFileAsByte = bos.toByteArray();// trying to refer this in SendMail Class
         workbook.close();
}}

public class SendMail{
public void sendMailWithAttachment(String excelFileName){
try{
        String recipient = "recipient@gmail.com";
     
        String sender = "sender@gmail.com";

        String host = "127.0.0.1";

        Properties properties = System.getProperties();

        properties.setProperty("mail.smtp.host", host);
    
        Session session = Session.getDefaultInstance(properties);
     
            MimeMessage message = new MimeMessage(session);
     
            message.setFrom(new InternetAddress(sender));
     
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
     
            message.setSubject("This is Subject");
             
            BodyPart messageBodyPart1 = new MimeBodyPart();
            messageBodyPart1.setText("This is body of the mail");
             
            BodyPart messageBodyPart2 = new MimeBodyPart();
            //DataSource source = new FileDataSource(filename); 
            //messageBodyPart2.setDataHandler(new DataHandler(source)); 
            ByteArrayResource resource = new ByteArrayResource(excelFileAsByte);
            messageBodyPart2.setFileName(excelFileName);
 
             
            Multipart multipartObject = new MimeMultipart(); 
            multipartObject.addBodyPart(messageBodyPart1); 
            multipartObject.addBodyPart(messageBodyPart2);
     
            message.setContent(multipartObject);
     
            Transport.send(message);
            System.out.println("Mail successfully sent");
        }
        catch (MessagingException mex)
        {
            mex.printStackTrace();
        }
    }}

how can i access excelFileAsByte in the other class and send the mail without storing it locally. And Can i add the file as an attachment using the addBodyPart and ByteArrayResource.

1 Answers

You will need a field. This is a very common thing in Java. Add below to the class ExcelFile. This is known as property setters and getters.

public class ExcelFile {
    private byte[] excelFileAsByte;

    public byte[] getExcelFileAsByte() {
        return excelFileAsByte;
    }

    public void setExcelFileAsByte(byte[] excelFileAsByte) {
        this.excelFileAsByte = excelFileAsByte;
    }

    ...

}

And also:

  • Remove static from generateExcelFile.
  • Change byte[]excelFileAsByte = bos.toByteArray() to excelFileAsByte = bos.toByteArray().

In your other class, access it with

ExcelFile excelFile = new ExcelFile();
excelFile.generateExcelFile(...
byte[] myExcelFile = excelFile.getExcelFileAsByte();

This is not a complete example, but I hope it will get you started.

Related