function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam SwannSam Swann 

Sending email via Apex class

Hi

Can someone help me with sending an email via Apex class?

Scenario:
Recipient: sammypuppy09876@gmail.com
Sender: no_reply@sammypuppy.com
Subject: "User Login Failed" 
Text: "My Custom Text with one field from above user record" 

I did workflow as well to sent out the email, but now I wanted to do same customization via Apex code (learning curve).

Thanks

Best Answer chosen by Sam Swann
Sam SwannSam Swann
Hi,

After a long research by myself, I found this Apex documentation which fulfills my basic requirement.
Please go through this link:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm

This all I needed. 
And this material might be helpful to someone who has the same requirement as mine. 

Thanks,
Sammy

All Answers

Lokesh KumarLokesh Kumar
HI Sam,

Please try the below code.

Apex Class :
 
public class singleEmailExample
{
public PageReference sendingEmail()
{
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
String[] sendingTo = new String[]{‘XXXXXXXXXXXXXX@gmail.com’}; 
semail.setToAddresses(sendingTo); 
String[] sendingToBccAdd = new String[]{‘XXXXXXXXX@gmail.com’}; 
semail.setBccAddresses(sendingToBccAdd); 
String[] sendingTocAdd = new String[]{‘XXXXXXXXXXX@gmail.com’}; 
semail.setCcAddresses(sendingTocAdd); 
semail.setSubject(‘Single Email message Example’); 
semail.setPlainTextBody(‘Hello!!!!!!!!!!This is a test email to test single email message program’); 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail}); 
return null; 
} 
}

VisualForce Page : 
 
<apex:page controller="singleEmailExample">
  <apex:form >
  <apex:commandButton value="SendEmail" action="{!sendingEmail}"/>
  </apex:form>
</apex:page>

let me know if this help you.

Happy to help You!

Thanks,
Lokesh
My Blog: http://sf2learn.blogspot.in 
Sam SwannSam Swann

Hi Lokesh,

Is it possible to NOT to use any VF pages for sending out email?
For instance:
In a Community login page, if a new user tries to login without having a User record in Salesforce, I should be notified immediately via Email. It should be configured in the Apex class.
Idea is, I would have a flag before giving the access to the commuinty. If flag=true, he/she should get to home screen. If not, I should get an email.

Is this configurable without VF pages?

Thanks.


 

Sam SwannSam Swann
Hi,

After a long research by myself, I found this Apex documentation which fulfills my basic requirement.
Please go through this link:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm

This all I needed. 
And this material might be helpful to someone who has the same requirement as mine. 

Thanks,
Sammy
This was selected as the best answer
Anshul Sahu 7Anshul Sahu 7
Hi,
send email through apex
your answer here 
Please go through this link:

https://supportsalesforce.blogspot.com/2022/08/email-send-through-apex-class.html

let me know if this help you.

Happy to help You!

Thanks,
Anshul
Amol waghAmol wagh
Hi,
Please refer to this (https://www.cloudwaale.com/post/how-to-send-email-from-apex-class) blog, here you will get step by step guide with examples.

Thanks,
Amol, www.cloudwaale.com