cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Send email with attachment via EmailService failes

Former Member
0 Likes
1,966

Hi there,

I'm trying to send an email with an attachment through the EmailService, unfortunatelly it fails everytime if I add the attachment...

My Code snippet:

    final CatalogVersionModel catalog = catalogVersionService.getCatalogVersion(CATALOGNAME, CATALOGVERSION);
 
     final EmailAddressModel addrToModel = new EmailAddressModel();
     addrToModel.setDisplayName(EMAILTO);
     addrToModel.setEmailAddress(EMAILTO);
 
     final EmailAddressModel addrFromModel = new EmailAddressModel();
     addrFromModel.setDisplayName(EMAILFROM);
     addrFromModel.setEmailAddress(EMAILFROM);
 
 
     EmailAttachmentModel mediaModel = new EmailAttachmentModel();
     mediaModel.setCode(filename);
     mediaModel.setCatalogVersion(catalog);
     mediaModel.setRemovable(Boolean.TRUE);
     mediaModel.setLocation(filename);
     modelService.save(mediaModel);
 
     final EmailMessageModel msgModel = new EmailMessageModel();
     msgModel.setToAddresses(Collections.singletonList(addrToModel));
     msgModel.setFromAddress(addrFromModel);
     msgModel.setSubject("Test Report");
     msgModel.setBody("blabalbla");
     msgModel.setReplyToAddress("noreply@test.ch");
 
     msgModel.setAttachments(Collections.singletonList(mediaModel));
 
     emailService.send(msgModel);

Is there a special think to know about attachment with the EmailService? Without the attachment the service is working...

Does anybody know how to deal with attachments?

Many Thanks!

Greetz

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes

Hello everybody,

thank you for your replies, I finally found a solution which is working for me.

I saw that the EmailService has a method calls "createEmailAttachment", which is creating an EmailAttachment... the code inside this method looks like that:

 @Override
 public EmailAttachmentModel createEmailAttachment(final DataInputStream masterDataStream, final String filename,
         final String mimeType)
 {
     final EmailAttachmentModel attachment = getModelService().create(EmailAttachmentModel.class);
     attachment.setCode(filename);
     attachment.setMime(mimeType);
     attachment.setRealFileName(filename);
     attachment.setCatalogVersion(getCatalogVersion());
     getModelService().save(attachment);

     getMediaService().setStreamForMedia(attachment, masterDataStream, filename, mimeType, getEmailAttachmentsMediaFolder());
     return attachment;
 }

If you want to use this method it's important set the right Catalogversion before...

Greetz

Former Member
0 Likes

Hi Dominik, Can you tell me which catalog version are you using? In my case I called createEmalAttachment(with expected parameters) method but attachment is not coming in email. It works without attachment. Can you suggest anything else I have to do?

Former Member
0 Likes

Hi Dominik,

How can we set Catalogversion to use this emailAttachmentModel?

ansellsilvans
Participant
0 Likes

Hi ,

I have one clarification here. the above piece of code is fine for me but can we pass the InputStream directly from MultiPartFile .

I mean without creating media model.

Regards, Ansell

Answers (2)

Answers (2)

Former Member
0 Likes

Hi Dominic,

Try to override DefaultEmailGenerationService: method generate. Then create your attachment and add it to your emailMessageModel. You should have the mediaModel in DataBase. I hope this will help you.

 private void constructAttachments(final EmailMessageModel emailMessageModel,
                                                          final MediaModel file,
                                                          ) {
         final EmailAttachmentModel attachment = getModelService().create(EmailAttachmentModel.class);
         attachment.setCode(emailMessageModel.getPk().toString());
         attachment.setMime(file.getMime());
         attachment.setRealFileName(file.getRealFileName());
         attachment.setCatalogVersion(file.getCatalogVersion());
         attachment.setLocation(file.getLocation());
         attachment.setSize(file.getSize());
         getModelService().save(attachment);
         final List<EmailAttachmentModel> attachements = new ArrayList<>();
         attachements.add(attachment);
         emailMessageModel.setAttachments(attachements);
         

Best regards Hassan

Former Member
0 Likes

I think you need to set a mediamodel that you loaded so that it contains the PK reference to the instance.

So you need to save it and then load it and add that to the email.