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
vlad_BLvlad_BL 

Email with Attachment

Hi, guys

 

I have to send e-mail with attachment and to tell the truth I do succeed with this.

I do get e-mails with HTML file attached.

 

But... no matter what content-type has my attachment, I get html file which contains CONTENT meta directive that leads me to the servlet which downloads attachment as soon as I open this HTML.

 

What I really what to get, is the physical file attachment itself or just somehow to retrieve the link that I get in HTML.

 

Can anyone help me with this?

CLKCLK

can u plz explain how u r sending the mail?

through code?

put ur code

vlad_BLvlad_BL

Sure, below is the snippet of the method that sends the e-mail

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment interviewPDF = new Messaging.EmailFileAttachment();
		
PageReference pdf = Page.vf_EthiopianPDF;
Blob body;
		
String[] toAddresses = new String[] {'mail@test.com'};
mail.setToAddresses(toAddresses);
		
// Specify the address used when the recipients reply to the email. 
mail.setReplyTo('mail@test.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.
mail.setSubject('New Family : ' + currentAccount.Name + ' has been interviewed');
		
// Specify the text content of the email.
mail.setHtmlBody('Family <b>' + currentAccount.Name +'</b> has been interviewed');
pdf.getParameters().put('id', currentAccount.Id);
body = pdf.getContent();
		
interviewPDF.setBody(body);
interviewPDF.setContentType('application/pdf');
interviewPDF.setFileName(currentAccount.Name + ' Interview');
interviewPDF.setInline(false);

mail.setFileAttachments(attachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

 

 

CLKCLK

did u try it withh setInline proprty to true for emailFileAttachment object.

vlad_BLvlad_BL

Yes, the same results

CLKCLK

good question, boss

I appretiate ur way of fulfilling the requirement , but no idea sir ji.

really sorry for taking ur time.

marioluisscmarioluissc

Hi Guys,

I already worked with Emails in Apex. See my Apex Code bellow:

 public PageReference send() {
    
    // Create email.  
    
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        
    PageReference pdf =  Page.quotePDF;
    pdf.getParameters().put('id',(String)cota.id); 
    pdf.setRedirect(true);
    Blob b;
   

// Get Page content as PDF.

    try{
      b = pdf.getContent();
      b = pdf.getContentAsPDF();
    }catch(VisualforceException erro){
      b = Blob.valueOf('Error in Test Class');  
    }
    
    
    // Create email attachment
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('Cotacao.pdf');
    efa.setBody(b);
    String addresses;
    if (cota.Contato__r.Email != null)
       {addresses = cota.Contato__r.Email;
    }
    String[] toAddresses = addresses.split(':', 0);
  
  // Set email parameters.
    email.setSubject( subject );
    email.setToAddresses( toAddresses );
    email.setPlainTextBody( body );
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

    // Send Email
    
    Messaging.SendEmailResult [] r = 
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
    
    if (r[0].isSuccess()){
      emailEnviado = true;
      emailErro = false;
      emailCorpo = false;
System.Debug('Email sent')
    }else{
      emailEnviado = false;
      emailErro = true;
      emailCorpo = false;
System.debug('Error: email not sent')
    }
   
Pagereference pageconta = new Pagereference('/'+cota.Cotacoes__r.Id);
    
    return null;
  }
vlad_BLvlad_BL

Hi, marioluissc

 

I tried even this, anyway I get HTML document

marioluisscmarioluissc

OK, but...

I didn't understand what you really want.

 

do you want a PDF attach on Email?

 

or

 

do you want a HTML page in email body?

vlad_BLvlad_BL

To marioluissc

 

I want to get strictly PDF file itself, but I get HTML with something similar inside:

 

 

<html><head><meta http-equiv="Refresh" content="0; URL=http://STORAGE.salesforce.com/servlet/servlet.EmailAttachmentDownload?q=5Kctzpo5P5%2B2JXytFukFWAn%2FKYcV%2F2AtCufODmeGWAHz8eoPbJUEEOQKmr1I86G7zzoBorRy2pEZtvvmzpSazg%3D%3D"></head></html>

 

 

As soon as I open the HTML that I get, it download PDF.

Did I make myself clear?

marioluisscmarioluissc

OK i understood.

 

do you can show your code again? also of  Pdf page.

marioluisscmarioluissc

Try change your code with red observation:

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment interviewPDF = new Messaging.EmailFileAttachment();

PageReference pdf = Page.vf_EthiopianPDF;
Blob body;

String[] toAddresses = new String[] {'mail@test.com'};
mail.setToAddresses(toAddresses);

// Specify the address used when the recipients reply to the email.
mail.setReplyTo('mail@test.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.
mail.setSubject('New Family : ' + currentAccount.Name + ' has been interviewed');

// Specify the text content of the email.
mail.setHtmlBody('Family <b>' + currentAccount.Name +'</b> has been interviewed');
pdf.getParameters().put('id', currentAccount.Id);
body = pdf.getContent();
body = pdf.getContentAsPDF();

interviewPDF.setBody(body);
interviewPDF.setContentType('application/pdf');
interviewPDF.setFileName(currentAccount.Name + ' Interview.pdf');
interviewPDF.setInline(false);

mail.setFileAttachments(new Messaging.EmailFileAttachment[] {interviewPDF});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
Try Try ccxvzcvxMessaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment interviewPDF = new Messaging.EmailFileAttachment();

PageReference pdf = Page.vf_EthiopianPDF;
Blob body;

String[] toAddresses = new String[] {'mail@test.com'};
mail.setToAddresses(toAddresses);

// Specify the address used when the recipients reply to the email.
mail.setReplyTo('mail@test.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.
mail.setSubject('New Family : ' + currentAccount.Name + ' has been interviewed');

// Specify the text content of the email.
mail.setHtmlBody('Family <b>' + currentAccount.Name +'</b> has been interviewed');
pdf.getParameters().put('id', currentAccount.Id);
body = pdf.getContent();

interviewPDF.setBody(body);
interviewPDF.setContentType('application/pdf');
interviewPDF.setFileName(currentAccount.Name + ' Interview');
interviewPDF.setInline(false);

mail.setFileAttachments(attachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
vlad_BLvlad_BL

The same behavior...

I have sent you the fragments of controller and pdf page, check them out if you have some time.

 

I really appreciate your time,