I want to be able to access the File Paths of a specific ID stored in my DB(SSMS) and send it in an email(email with multiple file) through my API

Viewed 16

This is my code. So I'm using JDBC template to be able to connect with my database which has the Path to a file.The TABLE in my DB which has the Path. get This is my code(part1)This is my code(part).

package demoExample;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



@RestController
public class SendingMultipleFilesThroughEmail {
int latestPathJbID;
@Autowired  
private JavaMailSender mailSender;
@Autowired
private JdbcTemplate jdbcTemplate;


@RequestMapping(value = "/MultipleAttachmentEmail", method = RequestMethod.POST ,consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity sendMultipleAttachmentEmail(@RequestPart("email") EmailContent emailContent, MultipartFile[] files, @RequestPart("Path")PathDetails Path) throws MessagingException

The reason im using @RequestPart is to be able to get input from my postman

{
    latestPathJbID = Path.getJbID();
    System.out.println(latestPathJbID);
    
    String sql = "Select EmpPath FROM PATH WHERE JbID = ?" ;

Im using the JbID to get the Path from my DB

    List<Map<String, Object>> employeePath = jdbcTemplate.queryForList(sql, latestPathJbID);
    Over here i want to use arrays because since each JbID(from my DB table) have the potential to have more than 1 path
    
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
    
    mimeMessageHelper.setFrom("xxxxxxxxxxxxxx@gmail.com");
    mimeMessageHelper.setTo(emailContent.getToEmail());
    mimeMessageHelper.setText(emailContent.getBody());
    mimeMessageHelper.setSubject(emailContent.getSubject());
    

This is where the main issue comes since im not able to declare an array i am not able to navigate through each path.

    for(int i = 0; i < employeePath.size(); i++)
    {
        try
        {
        String fileName =  employeePath.getOriginalFilename();
        //system.out.println(fileName);
        
        FileSystemResource fileSystem = new FileSystemResource("C:/Users/robin/Desktop/"+ fileName);
        mimeMessageHelper.addAttachment(fileSystem.getFilename(), fileSystem);
        mailSender.send(mimeMessage);
        //System.out.println(fileSystem);   
        }
        catch(Exception e)
        {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    return ResponseEntity.ok("Files transfered Succesfully...");
    
    
    
}

}

This is my PathDetails class where im getting and setting the Path details

package demoExample;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
@Table(name="PATH")
public class PathDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    
    private int JbID;
    private String EmpPath;
    

    public PathDetails( int JbID, String EmpPath)
    {
        
        this.JbID = JbID;
        this.EmpPath = EmpPath;
        
    }

    @JsonProperty("JbID")
    public int getJbID() {
        return JbID;
    }

    public void setJbID(int JbID) {
        this.JbID = JbID;
    }
    
    
    public String getEmpPath() {
        return EmpPath;
    }

    public void setEmpPath(String EmpPath) {
        this.EmpPath = EmpPath;
    }
}

Do take a look at the pictures for more clarification and Thanks in advance for helping me out.

0 Answers
Related