You need to sign in to do that
Don't have an account?
Naresh Krishna.ax1176
Email with attachment
Hi,
I wolud like to send an email with attachment from a visual force page. I am able to send an email by manually selecting the attachment from the local system.
My requirement is to automatically have the document attached and then send the email.
Could any one guide me on how to do this.
Thanks in advance.
PageReference PageRef = Page.ProdQuotes2;
PageRef.getParameters().put('id',strOpId);
PageRef.setRedirect(true);
Blob blobContent = PageRef.getContent();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Quote Mail ');
mail.setHtmlBody(blobContent.tostring());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Hi,
Try the below code sample:
<apex:page controller="EmailFromDocument">
<apex:form >
<table>
<tr>
<td><apex:outputLabel >Email ID</apex:outputLabel></td>
<td><apex:inputText value="{!emailid}"/></td>
</tr>
<tr>
<td colspan="2"><apex:commandButton value="sendmail" action="{!sendemail}"/></td>
</tr>
</table>
</apex:form>
</apex:page>
=============================Apex Controller======================
public class EmailFromDocument {
public String emailid { get; set; }
public void sendemail()
{
String[] addr=new String[]{emailid };
List<String> docid=new List<String>{'01590000000Nrat'}; // Give the document ID here
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject( 'Document As Attchment' );
email.setToAddresses( addr );
email.setPlainTextBody( 'Text attahment demo' );
email.setDocumentAttachments(docid);
// email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
public EmailFromDocument ()
{
/*
PageReference pdf = page.PDF_Of_TextArea;
pdf.getParameters().put('tx',mergedText);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Reference the attachment page and pass in the account ID
// pdf.setRedirect(true);
// Take the PDF content
Blob b = pdf.getContent();
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('TextAttachment1.pdf');
efa.setBody(b);
// Sets the paramaters of the email
String[] addr=new String[]{'ajay572@g.com'};
email.setSubject( 'pdf attachment' );
email.setToAddresses( addr );
email.setPlainTextBody( 'Text attahment demo' );
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
*/
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Navatar,
Be warned that sending an email with a pdf attachment (via get content or get content as pdf) while also uploading a file on the form is spotty at best.
So as long as you aren't trying to upload a file your code should work fine.
I speak from personal experience and yes SFDC knows about this .....
Hi Navatar,
Thanks for your reply.
For example, I want to send an image as an attachment.
could you please let me know how to get the document ID ?
Hi Veru,
Thanks for your reply.
With your example i can able to send a mail with html body. But i want to send a mail with attachment (image file).