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
kabukabu 

Email attachment just sending the link and asking password userid when trying to open

 

Messaging.SingleEmailMessage[] emails =new List<Messaging.SingleEmailMessage>();

 

//List<Contact> lstContacts=[Select Id,EMail FROM Contact WHERE EMAIL!=null];

for(Contactcont :lstContacts) {

Blob b =null;

Pagereference page =new Pagereference('/' + 'apex/OpportunityReport?cid='+cont.Id );

page.setRedirect(true);

Messaging.SingleEmailMessage mail =newMessaging.SingleEmailMessage();

String [] toAddresses =newString[] {email};

mail.setToAddresses(toAddresses);

mail.setSubject('Opportunity Report');

mail.setPlainTextBody('Please see the attached html report');

 

// Create an email attachment

Messaging.EmailFileAttachment efa =new Messaging.EmailFileAttachment();

efa.setFileName(cont.Id+'report.html');

 

if (!Test.isRunningTest()){//unable to call .getContent() within tests

b = page.getContent();

}

else{

b = Blob.valueOf('Unit Test Attachment Body');

 

}

efa.setBody(b);

//attach the PDF /html

efa.setContentType('text/html');

mail.setFileAttachments(newMessaging.EmailFileAttachment[] {efa});

emails.add(mail);

 

}

 

Messaging.sendEmail(emails);

 

When I call this method  from a button click it send the attachment and I can see the attachment content is there but when I use batch apex and try to open this the link from eamail it asks user id and password always send me 2k filesize with links to salesforce page.

 

please let me know if you have comeacross simillar issue

 

Thanks

K

sfdcfoxsfdcfox
Mass email attachments, and any attachments over 3MB, are converted to "html" files that, when opened, should cause the file to download. I suspect that your page includes the headers/sidebar, etc. Salesforce.com includes some JavaScript logic that will make a user log in if their session is invalid instead of just showing the page. So, since you're rendering an entire page into a HTML file, and sending that, it's causing the user to have to log in.

Instead, render the file as PDF, or turn off the headers so that the JS won't cause the user to be redirected to the login page.
kabukabu

Hi SFDCFox,

Thanks for your response.

The file size even not MB it is 10KB to 100KB. I tried with Renderas PDF and  turned off the header and sidebar and did not work.

As I mentioned in my original post it works from button click. when I use the same email attachment thro' batch apex to send email, the VF page is not getting rendered to get the content. I think that is happening.

 

Can you see any other issue ?

 

thanks

K

sfdcfoxsfdcfox

As I stated previously, all mass email attachments are converted, regardless of their size.

kabukabu

is there any way you can suggest to send emails that would not ask user to login and should be without userid/passwords.??

 

 

thanks

K

sfdcfoxsfdcfox

My apologies for the late reply. You must strip the headers from the page before sending it. A salesforce.com page includes logic to automatically redirect to the login page when the session has expired and a page is loaded. I strongly recommend rendering as a PDF for best resuls.

kabukabu

getContent () does not work  in batch jobs even in pdf

 

http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_pagereference.htm

 

need to find a different way or suggest something else.

 

thanks

K