Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

java - Send email with attachment getting data from content bytea postgresql

I want to send attachments to all the recipients of email.

This is the Json.

{
 "webId": 1001,
 "clientId": 592571,
 "startDate": null,
 "endDate": null,
 "externalKey": null,
 "fromEmail": "[email protected]",
 "fromName": "Test",
 "subject": "hello new",
 "body": "This is a test mail new with attachment",
 "status": "SUCCESS",
 "sentDate": null,
 "sendAttempts": 1,
 "permanentFailure": false,
 "emailRecipientModel": [
 {
      "webId": 1101,
      "clientId": 592571,
      "startDate": null,
      "endDate": null,
      "emailId": 1001,
      "emailAddress": "[email protected]",
      "status": "PENDING",
      "smtpStatusCode": 123,
      "recipientType": "TO"
 },
 {
      "webId": 1102,
      "clientId": 592571,
      "startDate": null,
      "endDate": null,
      "emailId": 1001,
      "emailAddress": "[email protected]",
      "status": "PENDING",
      "smtpStatusCode": 123,
      "recipientType": "TO"
 }
 ],
 "emailAttachmentModel": [
 {
      "webId": 1201,
      "clientId": 592571,
      "emailId": 1001,
      "key": "Test Key",
      "location": "Test Location",
      "name": "TestName.txt",
      "content": "VkdocGN5QnBjeUJoSUhSbGMzUWdkR1Y0ZENCQmRIUmhZMmh0Wlc1MERRb05DbFJvYVhNZ2FYTWdZU0IwWlhOMElIUmxlSFFnUVhSMFlXTm9iV1Z1ZEEwS0RRcFVhR2x6SUdseklHRWdkR1Z6ZENCMFpYaDBJRUYwZEdGamFHMWxiblFOQ2cwS1ZHaHBjeUJwY3lCaElIUmxjM1FnZEdWNGRDQkJkSFJoWTJodFpXNTBEUW9OQ2xSb2FYTWdhWE1nWVNCMFpYTjBJSFJsZUhRZ1FYUjBZV05vYldWdWRBMEs="
 }
 ]
}

Now i have a text file on my PC which i have converted to BASE64 and set it to content field. Now how to get this content to email attachment.

This is my Function

public Email sendEmail(Email email){

    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    
    List<EmailConfig> emailConfigs = emailConfigRepository.findAllByClientWebId(email.getClient().getWebId());
    
    EmailConfig emailConfig;
    
    if (emailConfigs.iterator().hasNext()){
        emailConfig = emailConfigs.iterator().next();
    }else{
        throw new ResourceNotFoundException(HttpStatus.NOT_FOUND.getReasonPhrase());
    }
    
    javaMailSender.setHost(emailConfig.getHostName());
    javaMailSender.setPort(emailConfig.getPortNo());
    javaMailSender.setUsername(emailConfig.getEmail());
    javaMailSender.setPassword(emailConfig.getPassword());

    Properties javaMailProperties = new Properties();
    javaMailProperties.put("mail.smtp.starttls.enable", "true");
    javaMailProperties.put("mail.smtp.auth", "true");
    javaMailProperties.put("mail.transport.protocol", "smtp");
    javaMailProperties.put("mail.debug", "true");
    
    try{

        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(email.getFromEmail());

        String recipients = email.getEmailRecipient().stream()
            .map(EmailRecipient::getEmailAddress)
            .collect( Collectors.joining( "," ) );

        helper.setTo(InternetAddress.parse(recipients));
        helper.setSubject(email.getSubject());
        helper.setText(email.getBody());
        //helper.addAttachment(emailAttachment.getName(),file);        //need help here
        
        javaMailSender.setJavaMailProperties(javaMailProperties);
        javaMailSender.send(message);
    }catch (Exception exception){
        exception.printStackTrace();
        email.setStatus(EmailStatus.FAILED);
    }
    
    return email;
}

There will be a list of attachments so how to get that list and send it to all the recipients.

Help will be appreciated.

question from:https://stackoverflow.com/questions/65847321/send-email-with-attachment-getting-data-from-content-bytea-postgresql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create temp file:

File file = File.createTempFile(emailAttachment.getName(), ".tmp");
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(emailAttachment.getContent());
fileWriter.close();

and just use like this:

helper.addAttachment(emailAttachment.getName(), file);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...