You need to sign in to do that
Don't have an account?
Quanchi
Email Help - Apex Code
Hello,
I have followed the directions perfectly from the below link and everything works fine.....emails send.
http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_custom_controller.htm
I need to add a browse button to allow the user to add local files as attachments to the email. The email is not saved in Salesforce, it is just sent to the recipient.
I am having problems with the email.setFileAttachments portion of the class.
Any ideas?
Thanks,
Qaunchi
Percect!!!! Thanks for your help.
I need an If Statement that handles if the ccaddress is left empty. I tried the below, but it still doesn't work. And ideas?
public string ccemail { get; set; }
if(ccemail != Null){
String[] ccAddresses = new String[] {ccemail};
email.setCcAddresses( ccAddresses );
}
Thanks,
Quanchi
All Answers
Use the inputFile tag to create the file browsing functionality on your VF page and link the value to a Blob variable and filename to a String in your controller, like this:
Add this to Controller:
String fileName;
Blob fileBody;
Add this to your VF page:
<apex:inputFile value="{!fileBody}" filename="{!fileName}" />
In the send method of your controller create a new EmailFileAttachment object and use that in the setFileAttachments() method, like this:
// in your send method
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName( fileName );
efa.setBody( fileBody );
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setFileAttachments( new Messaging.EmailFileAttachment[] { efa } );
Hope that helps!
Clint
This was a huge help, but I do have one question.
I already had this working code to send the email in the bottom of my class:
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Where/how do I add the code to add the attachment to the email?
Thanks in advance.
Quanchi
Ok, I got it working!!!!!
Now for the final touches.....I want to let the user add two file attachments.
ideas? I have added the second input box to my VF page but just need some advice on the class.
Thanks,
Quanchi
I now get an error if there is no attachement.....works fine if there is an attachment.
Ok, I still need help. What is happening now is that if the user doesn't attach an attachment, I get an error. Please help is you can....I am stuck.
Thanks,
Quanchi
Below is my current code in my Apex Class:
public class sendEmail {
public String subject { get; set; }
public String note { get; set; }
public String fileName1 { get; set; }
public blob filebody1 { get; set; }
private final Opportunity Opportunity;
// Create a constructor that populates the opportunity object
public sendEmail() {
opportunity = [Select statement goes here from opportunity where id = :ApexPages.currentPage().getParameters().get('id')];
}
public opportunity getopportunity() {
return opportunity;
}
public PageReference send() {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'email@address.com'};
String[] bccAddresses = new String[] {'email2@address.com'};
String[] ccAddresses = new String[] {'email3@address.com};
email.setReplyTo('email4@address.com');
email.setSenderDisplayName('Salesforce');
email.setUseSignature(false);
// Sets the paramaters of the email
email.setSubject( 'New RFP - '+ Opportunity.Name + ' - '+ Opportunity.Owner_Name__c);
email.setToAddresses( toAddresses );
email.setCcAddresses( ccAddresses );
email.setbccAddresses( bccAddresses );
// Attachments
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.filename = null;
efa.body = null;
efa.setFileName(fileName1);
efa.setBody (fileBody1);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
email.setHtmlBody('email body goes here');
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
// redirects user to a thank you page after the email is sent.
{
PageReference p = Page.ThankYou;
p.setRedirect(true);
return p;
}
}
}
// Attachments
if(fileName1 != Null){
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.filename = null;
efa.body = null;
efa.setFileName(fileName1);
efa.setBody (fileBody1);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
}
Percect!!!! Thanks for your help.
I need an If Statement that handles if the ccaddress is left empty. I tried the below, but it still doesn't work. And ideas?
public string ccemail { get; set; }
if(ccemail != Null){
String[] ccAddresses = new String[] {ccemail};
email.setCcAddresses( ccAddresses );
}
Thanks,
Quanchi
Thanks for everybody's help, I got the below code to work:
if(ccemail != Null && ccemail !=''){
String[] ccAddresses = new String[] {ccemail};
email.setCcAddresses( ccAddresses );
}