You need to sign in to do that
Don't have an account?
Can extensions to standard controllers in VFP send E-Mails
I want a Visual Force Page to send an e-mail when it is opened and I tried to do this with an extension but it did not work.
Can an extension to a VFP which uses a standard controller send an e-mail?
I have tried the following.
1. The debug logs appear to show the messages being sent.
2. My triggers send e-mails to the same address so there is nothing wrong there.
3. I have sent messages to different servers so it is not the server.
4. The extension code executes without errors.
5. The e-mail addresses are correct.
My code is below, any suggestions would be grate thanks?
Visual Force Page
<apex:page sidebar="false" showHeader="false" extensions="EmailPDFController2" standardController="mycompany_Invoice__c">
<p>Sending Email</p>
</apex:page>
APEX Class
public class EmailPDFController2 {
public EmailPDFController2 (ApexPages.StandardController controller){
try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'test2@test2.com'};
String[] CopyToAddresses = new String[]{'test@test1.com','test3@test3.com'};
mail.setToAddresses(toAddresses);
mail.setCcAddresses(CopyToAddresses);
String msg = 'Hellow';
mail.setPlainTextBody(msg);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
} catch (Exception e){}
}
}
Log Entrhy
25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
11:30:10.071 (71679000)|EXECUTION_STARTED
11:30:10.071 (71737000)|CODE_UNIT_STARTED|[EXTERNAL]|066U0000000GXQs|VF: /apex/mycompanyInvoicePDF
11:30:10.074 (74704000)|CODE_UNIT_STARTED|[EXTERNAL]|01pU00000012Pwn|EmailPDFController2 <init>
11:30:10.074 (74722000)|SYSTEM_MODE_ENTER|true
11:30:10.075 (75730000)|METHOD_ENTRY|[1]|01pU00000012Pwn|EmailPDFController2.EmailPDFController2()
11:30:10.075 (75828000)|METHOD_EXIT|[1]|EmailPDFController2
11:30:10.076 (76837000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
11:30:10.076 (76875000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
11:30:10.076 (76910000)|SYSTEM_METHOD_ENTRY|[5]|LIST<String>.add(Object)
11:30:10.076 (76930000)|SYSTEM_METHOD_EXIT|[5]|LIST<String>.add(Object)
11:30:10.076 (76944000)|SYSTEM_CONSTRUCTOR_ENTRY|[6]|<init>()
11:30:10.076 (76965000)|SYSTEM_CONSTRUCTOR_EXIT|[6]|<init>()
11:30:10.076 (76985000)|SYSTEM_METHOD_ENTRY|[6]|LIST<String>.add(Object)
11:30:10.077 (77000000)|SYSTEM_METHOD_EXIT|[6]|LIST<String>.add(Object)
11:30:10.077 (77018000)|SYSTEM_METHOD_ENTRY|[6]|LIST<String>.add(Object)
11:30:10.077 (77032000)|SYSTEM_METHOD_EXIT|[6]|LIST<String>.add(Object)
11:30:10.077 (77270000)|SYSTEM_CONSTRUCTOR_ENTRY|[11]|<init>()
11:30:10.077 (77302000)|SYSTEM_CONSTRUCTOR_EXIT|[11]|<init>()
11:30:10.077 (77324000)|SYSTEM_METHOD_ENTRY|[11]|LIST<Messaging.SingleEmailMessage>.add(Object)
11:30:10.077 (77342000)|SYSTEM_METHOD_EXIT|[11]|LIST<Messaging.SingleEmailMessage>.add(Object)
11:30:10.077 (77389000)|SYSTEM_METHOD_ENTRY|[11]|Messaging.sendEmail(LIST<Messaging.Email>)
11:30:10.085 (85895000)|EMAIL_QUEUE|[11]|bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [test2@test2.com], ccAddresses: [test@test1.com, test3@test3.com], plainTextBody: Hellow,
11:30:10.086 (86044000)|SYSTEM_METHOD_EXIT|[11]|Messaging.sendEmail(LIST<Messaging.Email>)
11:30:10.086 (86068000)|CODE_UNIT_FINISHED|EmailPDFController2 <init>
11:30:10.649 (89096000)|CUMULATIVE_LIMIT_USAGE
11:30:10.649|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Number of script statements: 8 out of 200000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 10
Number of Email Invocations: 1 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10
11:30:10.649|CUMULATIVE_LIMIT_USAGE_END
11:30:10.089 (89131000)|CODE_UNIT_FINISHED|VF: /apex/mycompanyInvoicePDF
11:30:10.089 (89143000)|EXECUTION_FINISHED
The solution to this was to remove the code from the constructor of the extension and put it in a function which was then aclled by a button on the VFP. This then called a second VFP which rendered as PDF. The code is below.
Page 1 contains button called "Send Me as PDF"
Page 2 is page 1 with the renderas=""PDF" in the heading.
APEX Code Here
public with sharing class EmailPDFController {
public EmailPDFController (ApexPages.StandardController controller){ }
public void SendThisEmail(){
try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'firstname.lastname@MyCompany.ie'};
String[] CopyToAddresses = new String[]{'firstname.lastname@test.com','firstname.lastname@MyCompanygroup.eu'};
mail.setToAddresses(toAddresses);
mail.setCcAddresses(CopyToAddresses);
String msg = 'Hellow';
PageReference curpage = ApexPages.currentPage();
PageReference pdfPage = Page.MyCompanyInvoicePDF2;
String ParID = (string) curpage.getParameters().get('id');
pdfPage.getParameters().put('id',ParID);
Blob b = pdfPage.getContent();
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
efa.setBody(b); //attach the PDF
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
mail.setPlainTextBody(msg);
// mail.setHtmlBody(msg);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
System.debug('Email Results isSuccess = ' + r[0].IsSuccess());
Messaging.SendEmailError [] err_mail = r[0].getErrors();
} catch (Exception e){System.debug('ERROR:' + e);}
}
}
All Answers
May be check the email results-
Add the below statement after email send.
The result shows that the mail was sent successfully.
But it was not sent.
Hi,
There is a similar thread to this. The solution was to take the code out from the constructor and put it in a separate method. Then this method can be called from the action attribute of page or via a button or any other means.
http://boards.developerforce.com/t5/Visualforce-Development/Messaging-sendEmail-Not-Sending-Email/td-p/141045
The solution to this was to remove the code from the constructor of the extension and put it in a function which was then aclled by a button on the VFP. This then called a second VFP which rendered as PDF. The code is below.
Page 1 contains button called "Send Me as PDF"
Page 2 is page 1 with the renderas=""PDF" in the heading.
APEX Code Here
public with sharing class EmailPDFController {
public EmailPDFController (ApexPages.StandardController controller){ }
public void SendThisEmail(){
try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'firstname.lastname@MyCompany.ie'};
String[] CopyToAddresses = new String[]{'firstname.lastname@test.com','firstname.lastname@MyCompanygroup.eu'};
mail.setToAddresses(toAddresses);
mail.setCcAddresses(CopyToAddresses);
String msg = 'Hellow';
PageReference curpage = ApexPages.currentPage();
PageReference pdfPage = Page.MyCompanyInvoicePDF2;
String ParID = (string) curpage.getParameters().get('id');
pdfPage.getParameters().put('id',ParID);
Blob b = pdfPage.getContent();
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
efa.setBody(b); //attach the PDF
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
mail.setPlainTextBody(msg);
// mail.setHtmlBody(msg);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
System.debug('Email Results isSuccess = ' + r[0].IsSuccess());
Messaging.SendEmailError [] err_mail = r[0].getErrors();
} catch (Exception e){System.debug('ERROR:' + e);}
}
}