Be aware there are governor limits related to sending emails in Apex (10 emails per transaction).
Sending emails in triggers usually will exceed that limit when batches are posted. The workaround for that is to do DML in Apex that causes a Workflow to fire the email instead.
Here's a copy and paste from some of my stuff to get you started.... It only work for plain text. I do html too, but html emails are a little more complicated as you have to do DML then scheduled apex to send them 10 at a time if you want to avoid the governor limit.
Workflow:
Object: Apex Outbound Email
Evaluation Criteria: Every time a record is created or edited
Rule Criteria: Apex Outbound Email: Sent equals False
Workflow Actions: Email Alert (Send the Email) and Field Update (Sent to True)
Email Alert:
Recipients: Email Field: To
Object: Apex Outbound Email
Email Template:
Subject: {!Apex_Outbound_Email__c.Subject__c}
Body: {!Apex_Outbound_Email__c.Body__c}
Apex:
List<Apex_Outbound_Email__c> e_list = new List<Apex_Outbound_Email__c>();
Apex_Outbound_Email__c e = new Apex_Outbound_Email__c(); e.To__c=opp_map3.get(t.OwnerId); e.Subject__c='Standard 36 Hour Quote Follow Up'; e.Body__c='New Task\n\nSomeone has assigned you the following new task:\n\nSubject: Standard 36 Hour Quote Follow Up\nPriority: Normal\nQuote: '; e_list.add(e);
I copied exactly the outbound email Apex sample code from the link you provided. However I got an error message that says "Error: Compile Error: unexpected token: ')' at line 17 column 31" which it referenced this line mail.setToAddresses(toAddresses);.
Any ideas why it throw out that error? Please advice. thanks
// Create a new single email message object// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses =new String[] {'user@acme.com'};
String[] ccAddresses =new String[] {'smith@gmail.com'};
// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);
// Specify the address used when the recipients reply to the email.
mail.setReplyTo('support@acme.com');
// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');
// Specify the subject line for your email address.
mail.setSubject('New Case Created : '+case.Id);
// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);
// Optionally append the salesforce.com email signature to the email.// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);
// Specify the text content of the email.
mail.setPlainTextBody('Your Case: '+case.Id +' has been created');
mail.setHtmlBody('Your case:<b> '+case.Id +' </b>has been created<p>'+' View case <a href=https://na1.salesforce.com/'+case.Id+'>click here</a>');
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });
Look at the Messaging functions in the Apex language reference.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound.htm?SearchType=Stem&Highlight=messages|message|Messages|Message|Messaging|MESSAGE|messaging
Be aware there are governor limits related to sending emails in Apex (10 emails per transaction).
Sending emails in triggers usually will exceed that limit when batches are posted. The workaround for that is to do DML in Apex that causes a Workflow to fire the email instead.
Thanks for the tips. Do you have any sample codes for using DML to make workflow to fire email?
Regards,
Paul
Here's a blog post I did on apex emails and governor limits:
http://www.soliantconsulting.com/blog/2009/07/how-to-avoid-governor-limits-with-sendemail-in-apex/
Here's a copy and paste from some of my stuff to get you started.... It only work for plain text. I do html too, but html emails are a little more complicated as you have to do DML then scheduled apex to send them 10 at a time if you want to avoid the governor limit.
Workflow:
Object: Apex Outbound Email
Evaluation Criteria: Every time a record is created or edited
Rule Criteria: Apex Outbound Email: Sent equals False
Workflow Actions: Email Alert (Send the Email) and Field Update (Sent to True)
Email Alert:
Recipients: Email Field: To
Object: Apex Outbound Email
Email Template:
Subject: {!Apex_Outbound_Email__c.Subject__c}
Body: {!Apex_Outbound_Email__c.Body__c}
Apex:
List<Apex_Outbound_Email__c> e_list = new List<Apex_Outbound_Email__c>();
Apex_Outbound_Email__c e = new Apex_Outbound_Email__c();
e.To__c=opp_map3.get(t.OwnerId);
e.Subject__c='Standard 36 Hour Quote Follow Up';
e.Body__c='New Task\n\nSomeone has assigned you the following new task:\n\nSubject: Standard 36 Hour Quote Follow Up\nPriority: Normal\nQuote: ';
e_list.add(e);
insert e_list;