You need to sign in to do that
Don't have an account?
Anto Hotelbeds
Sending Email with Attachments: attachment no format
Hi,
I need to send an attachment via email through apex.
What I do in my code is create a case, then request for the attachment and once the attachment is introduces I send the email. This attachment can be in multiple formats (xls, txt,...).
I am able to send the attachment, but not in the correct format...Can you tell me please what I am doing wrong?
public PageReference uploadAndSendEmail() { //Upload the file attachment.OwnerId = UserInfo.getUserId(); attachment.ParentId = case1.Id; // the record the file is attached to attachment.IsPrivate = false; //I get the intranet Id of the user User u=[SELECT Id,Intranet_ID__c FROM User WHERE Id=:case2.OwnerId]; string interfaceId=u.Intranet_ID__c; if (attachment.Body!=NULL){ try { //Introduce the attachment insert attachment; //and create and send email Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'a.***@****.com'}; mail.setToAddresses(toAddresses); Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1]; Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment(); fileAttachment.setBody(attachment.body); fileAttachment.setFileName(attachment.name); fileattachment.setContentType(attachment.ContentType); fileAttachments[0] = fileAttachment; mail.setFileAttachments(fileAttachments); mail.setSubject('New Request: ' + Case2.Subject + ' - ' + Case2.Id + ' - PROJ=32 - ISSUE='+Case2.Footprints_ID__c);// New Request: {!Case.RecordType} - {!Case.Id} - PROJ=32 - ISSUE={!Case.Footprints_ID__c} mail.setPlainTextBody('Sender= '+string.valueOf(u.Intranet_ID__c)+'\n'+ 'New Category='+Case2.Footprints_Category__c+'\n'+ 'NewSubcategory='+Case2.Footprints_SubCategory__c+'\n'+ 'SFID='+Case2.Id+'\n'+ 'SF=Yes\n'+ 'Atlas Code: '+ Case2.Atlas_Code__c+'\n'+ 'Ebilling Email: '+ Case2.E_billing_Email__c+'\n'+ 'Ebilling Frequency: '+ Case2.E_billing_Frequency__c+'\n'+ 'Comments: '+ Case2.Comments_for_Admin__c); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment')); return null; } finally { attachment = new Attachment(); } }else{ //DOESNT MATTER. Sends a different email } } //Fin attachments
Any help is appreciated.
Thanks!
Antonio
Hi,
Refer exapmle given in below link to specify the format of the attachment:
http://www.mindfiresolutions.com/Sending-Mass-Email-through-Apex-Controllers-2344.php
Hi,
I keep receiving the email with no format in the attachment. The attachment is an excel file and is correctly saved with excel format in the case.
I did what you indicated, here is the code:
Thanks
Antonio