I have a page where the user selects content (that is 5MB or lower, only 2 attachments allowed per email) to use as an attachment in an email. It all seems to work fine, the email sends and there are attachments in it with the correct title. But the attachments themselves seems to be broken. There is nothing in them, or i get Forbidden Error 403 when i try to download them.
I have tried it will 3 types of content (power_point_x, power_point, pdf) and all fail like this. also when looking at the attachment in google drive, in the details section it says all three have a type of Mime type: application/octet-stream ? I know i am missing something simple here I am sure lol, just looking for some guidance.
Thanks!
List<Messaging.EmailFileAttachment> attList = new List<Messaging.EmailFileAttachment>();
Messaging.EmailFileAttachment efa;
if (!selectedContent.isEmpty()) {
for (ContentVersion cv : PromoModel_Helper.findSelected(selectedContent)) {
if (cv != null) {
efa = new Messaging.EmailFileAttachment();
efa.setFileName(cv.Title);
efa.setBody(cv.VersionData);
attList.add(efa);
}
}
}
mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(sendToIds);
mail.setReplyTo(replyToEmail);
mail.setSenderDisplayName(senderDisplayName);
mail.setBccSender(bcc);
mail.setUseSignature(signature);
mail.setSubject(subject);
mail.setHtmlBody(body);
if (!attList.isEmpty()) {
mail.setFileAttachments(attList);
}
try {
List<Messaging.SendEmailResult> results = Messaging.sendEmail(
new List<Messaging.Email> { mail }
);
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(
ApexPages.Severity.ERROR,
'An Error occured while trying to email, ' + e.getMessage()
));
return null;
}
Update:
So i tried adding efa.setContentType(cv.FileType);
which created an error saying the Messaging.SingleEmailMessage
was Not Serializable, so I tried making it transient
. That created an error around the file type:
An Error occured while trying to email, SendEmail failed.
First exception on row 0; first error: INVALID_CONTENT_TYPE, An invalid value was specified for contentType.: []
The output of the cv.FileType is typically (PDF, POWER_POINT_X, POWER_POINT, WORD, EXCEL_X) and this seems to not work (given the error above, and yes i checked that it was getting the value i expected) So I tried converting it to file extensions
efa.setContentType(
(cv.FileType == 'POWER_POINT_X' ?
'.pptx' : cv.FileType == 'POWER_POINT' ?
'.ppt' : cv.FileType == 'PDF' ?
'.pdf' : cv.FileType == 'EXCEL' ?
'.xls' : '.doc')
);
and still no joy :-(