given below is the Apex code for sending Word Form as an mail attachment.
List<Messaging.SingleEmailMessage> EmailMessages= new List<Messaging.SingleEmailMessage>(); List<User> loggedUser = [Select Id,Name,Email from User where Id=:UserInfo.getUserId()]; Messaging.SingleEmailMessage EmailMsg = new Messaging.SingleEmailMessage();//define mail body content -begin (here i used html tags to format my mail body because I am setting htmbodyString EmailBody = '<html><body> Dear User,<br/><br/>'; EmailBody ='Please Fill the attached document and send us'; EmailBody += '<br/><br/>Thank you,<br/>'+loggedUser[0].Name+'<br/><br/>'; EmailBody += '</body></html>';//define mail body content -endEmailMsg.setHtmlBody(EmailBody);// set the html body contnetEmailMsg.setToAddresses(new String[]{'mailaddreses seperated by commas'});//set recipient mail addressesEmailMsg.setSubject('SalesforceCat Demo'); EmailMsg.setReplyTo(loggedUser[0].Email);// set reply to email address becuase I expect this mailed to be repliedEmailMsg.setUseSignature(true); PageReference Word = new PageReference('/apex/WordDownloadExample');// url of ur visualforce page rendered as a word//Word.getParameters().put('Id','xxxx'); if any parameters need to be passed to the pageWord.setRedirect(true);//do a virtual redirect to the page so we can get page content using page referenceBlob b; b = Word.getContent();// get the content of the page and assignment to blobMessaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();// create an attachment instanceattachment.setBody(b);// set the blob as attachment contentattachment.setFileName('attach.doc');// set the attachment nameEmailMsg.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment});// link the attachment to the mailEmailMessages.add(EmailMsg); if(!Test.isRunningTest()) { Messaging.sendEmail(EmailMessages); }
Mail with word form as an attachment
![]() |
| mail sent |

No comments:
Post a Comment