on ‎2015 May 08 5:01 PM
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
Request clarification before answering.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.