0

So I have my own java springboot code that writes an excel spreadsheet with a List from my local directory. Basically it just takes the list of files in a directory I want, and then lists out the contents of each zip file in the directory into an excel spreadsheet. And then I use FileOutputStream to generate that XLS into another directory. The code look something like:

HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Test sheet");
/*
...
insert code to create the rows and cells of the workbook/sheet
...
*/

FileOutputStream fileOutputStream = new FileOutputStream("/test/random/excelspreadsheets")
workbook.write(fileOutputStream);
fileOutputStream.close();

So this writes the xls, generates it and saves it into specified directory, but my question is, how do I send this as an email, with a subject, cc, from, to, and most importantly as an attachment to the email to specified either my email or friends email? I've been scratching my head at this for the past day and have no idea, it just seems obscure to me. Thank you!

stackerstack
  • 243
  • 4
  • 16
  • The code in [this question](https://stackoverflow.com/questions/64102830/how-to-send-a-file-via-email-without-storing-it-in-storage-first-using-spring-bo) shows how to send an Excel file via e-mail, given the file has already been created and written to disk. The answer then shows how to do this without needing to write the file to disk first - but it sounds like you want the code from the _question_, not from the _answer_. – andrewJames Dec 09 '20 at 20:13
  • wait, what are you trying to say again? I'm confused about "but it sounds you want the code from the question, not from the answer" – stackerstack Dec 09 '20 at 20:14
  • Look at the code provided in the linked question rather than the code provided in the answer to that question. The starting point of the person who asked that question sounds like what you need your solution to be. – andrewJames Dec 09 '20 at 20:18

1 Answers1

0

First add this dependency

    <!-- Email -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>

Then you can use the following service to send this email

@Service
public class EmailService {
private JavaMailSender emailSender;

@Autowired
public EmailService(JavaMailSender emailSender) {
    this.emailSender = emailSender;
}

public void sendEmail() {

    String to = "recepient@gmail.com";
    String subject = "email subject";
    String text = "email text";

    MimeMessage message = emailSender.createMimeMessage();

    //TODO here convert your workbook to a xlsx file 
    File file = new File("yourFile.xlsx");
    if (file.exists() && !file.isDirectory()) {

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);

            helper.addAttachment("yourFile.xlsx", file);
        } catch (Exception ex) {
        System.out.println("Error");
        }
    }

    emailSender.send(message);
}
Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47