Email Programs by using Apex

When we want to send an email to the external system from salesforce or receive the email from the external system then we use email programs. There are two types of emails.

  1. Outbound email messaging.
  2. Inbound email messaging

Outbound email programs

Outbound email programs are used to send an email to an external system using apex. There are two types of outbound emails.

1. SingleEmailMessage: Used to send a single email message.


Messaging.SingleEmailMessage varEmail = new Messaging.SingleEmailMessage();

2. MassEmailMessage: By using this we can send email to a list of recipients.


Messaging.MassEmailMessage varMemail = new Messaging.MassEmailMessage();

Messaging namespace provides classes/methods for salesforce outbound & inbound functionalities.

Sending with Attachments

How can we send attachments? We can send attachments by using EmailFileAttachment. We will see examples later.

Inbound email programs

This will receive an email from the external system to Salesforce and the apex class will process the email, attachments and perform requested operations.