function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sjain1.2523113033806995E12sjain1.2523113033806995E12 

Unable to view PDF when send an email with attach PDF

Hi guys,

 

when i am sending an email through Messaging.singleEmailMessage(Outbound Email) with attach pdf, unable to view pdf due to improper content type. I am not using the renderAsPdf content which is available in doc(Apex code documentation)

only set the data in attachment body of two textbox data.Anyone help me out to resolve this issues

 

public String subject { get; set; }
public String body { get; set; }
String inputStr = subject + body;
Blob inputdata = Blob.valueOf(inputStr);
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setContentType('application/pdf');
efa.setFileName('attachment.pdf');
efa.setBody(inputdata);

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = addresses.split(':', 0);
email.setSubject( subject );
email.setToAddresses(toAddresses);
email.setPlainTextBody(Msg);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

 

 

 

Regards

saurabh

 

Afzal MohammadAfzal Mohammad

I think the inputdata you are supplying is in incorrect format.

 

Try get the blob content from a vf page instead. I haven't tried it, hopefully it should work.

 

 

Blob inputdata = ApexPages.YOUR_VF_PAGE.getContentAsPDF();
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setContentType('application/pdf');
efa.setFileName('attachment.pdf');
efa.setBody(inputdata);

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = addresses.split(':', 0);
email.setSubject( subject );
email.setToAddresses(toAddresses);
email.setPlainTextBody(Msg);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

 

 

Hope that helps.

 

Afzal

sjain1.2523113033806995E12sjain1.2523113033806995E12

i donot want the blob content directly from visual force page so my requirement is to set the blob content which is coming from two textbox and pass into the setbody(b).

 

string inputdata  = string1+string2;

blob b= blob.valueOf(inputdata);

efa.setbody(b);