+ Start a Discussion
khushbu dubeykhushbu dubey 

hi..I have a doubt in this code that it can send email with attachment..

public class emailsend
{

public string subject{get; set;}
public string emailbody{get;set;}
public list<string> emails=new list<string>{'dubeykhushbu131191@gmail.com'};
public string attachment{get;set;}
public pagereference send()
{
messaging.singleemailmessage email=new messaging.singleemailmessage();
email.setsubject(subject);
email.setplaintextbody(emailbody);
email.settoaddresses(emails);
email.setccaddresses(emails);
email.setbccaddresses(emails);
messaging.sendemailresult[] r=messaging.sendemail(new messaging.singleemailmessage[]{email});
// Messaging.EmailFileAttachment attachfile= new Messaging.EmailFileAttachment();
// attachfile.setbody(attachment);
return null;
}
list<document> doc=new list<document>();
document d=new document();
public pagereference save(){
d.Folderid='00l28000001ELDU';
d.name=attachment;
insert d;
 doc=[select  folderid  from document where Folderid='00l28000001ELDU' ];
return null;}
}

 
Rupali PophaliyaRupali Pophaliya
To create an attachment, you need to use the Apex Messaging.EmailFileAttachment class. You must define both the file name and the content of an EmailFileAttachment object.

Refer this link for more details:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_sending_attachments.htm
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for Email Attachment.
1) http://sfdcsrini.blogspot.com/2014/06/sending-email-with-attachment-using.html
2) http://www.salesforcetutorial.com/sending-email-attachment-emailfileattachment/

Sample code.
public pagereference sendAttach()
{
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
PageReference pref = page.bookdetails;
pref.getParameters().put(‘id’,(String)book.id);
pref.setRedirect(true);
Blob b = pref.getContent();
attach.setFileName(‘BookDetails.pdf’);
attach.setBody(b);
semail.setSubject(‘Book details’);
String[] sendTo = new String[]{‘XXXXXXXXX@gmail.com’};
semail.setToAddresses(sendTo);
semail.setPlainTextBody(‘Please find the attached book details’);
semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
return null;
}
Let us know if this will help you

Thanks
Amit Chaudhary
khushbu dubeykhushbu dubey
Sorry..but it is not working...
 
Mahesh DMahesh D
Hi Khushbu,

Please find the below code to send an email with attachment.
 
public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }

    private final Account account;

    // Create a constructor that populates the Account object
    public sendEmail() {
        account = [SELECT Name, 
                  (SELECT Contact.Name, Contact.Email FROM Account.Contacts) 
                   FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference send() {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        // Reference the attachment page and pass in the account ID
        PageReference pdf =  Page.attachmentPDF;
        pdf.getParameters().put('id',(String)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('attachment.pdf');
        efa.setBody(b);

        String addresses;
        if (account.Contacts[0].Email != null) {
            addresses = account.Contacts[0].Email;
            // Loop through the whole list of contacts and their emails
            for (Integer i = 1; i < account.Contacts.size(); i++) {
                if (account.Contacts[i].Email != null) {
                    addresses += ':' + account.Contacts[i].Email;
                }
            }
        }

        String[] toAddresses = addresses.split(':', 0);

        // Sets the paramaters of the email
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body );

        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        // Sends the email
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
		
        return null;
    }
}
Also go through the below links:

http://www.salesforcetutorial.com/sending-email-attachment-emailfileattachment/

http://salesforce.stackexchange.com/questions/25115/send-email-with-attachment-dynamically

http://blog.jeffdouglas.com/2010/07/22/create-and-email-a-document-with-salesforce-com/


Please do let me know if it helps you.

Regards,
Mahesh

 
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below and try
public class emailsend
{

	public string subject{get; set;}
	public string emailbody{get;set;}
	public list<string> emails=new list<string>{'dubeykhushbu131191@gmail.com'};
	public string attachment{get;set;}
	public pagereference send()
	{
		Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
		String myString = 'StringToBlob';
		Blob myBlob = Blob.valueof(myString);
		attach.setFileName(‘BookDetails.pdf’);
		attach.setBody(myBlob);
		
		// Try below code if you want to send VF page as attachment
		//PageReference pref = page.bookdetails;		
		//pref.getParameters().put(‘id’,(String)book.id);
		//pref.setRedirect(true);
		//Blob b = pref.getContent();
		//attach.setBody(b)

		messaging.singleemailmessage email=new messaging.singleemailmessage();
		email.setsubject(subject);
		email.setplaintextbody(emailbody);
		email.settoaddresses(emails);
		email.setccaddresses(emails);
		email.setbccaddresses(emails);
		email.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});

		messaging.sendemailresult[] r=messaging.sendemail(new messaging.singleemailmessage[]{email});
		
		return null;
	}
	list<document> doc=new list<document>();
	document d=new document();
	public pagereference save(){
		d.Folderid='00l28000001ELDU';
		d.name=attachment;
		insert d;
		 doc=[select  folderid  from document where Folderid='00l28000001ELDU' ];
		return null;
	}

}
Let us know if this will help you

Thanks
Amit Chaudhary