Sending emails by using SingleEmailMessage

Sending emails

By using Single email message we can send emails. SingleEmailMessage class contains methods for sending emails. SingleEmailMessage contains below methods.

  1. setBccAddresses(String[])
  2. setCcAddresses(String[])
  3. setCharset(String)
  4. setDocumentAttachments(ID[])
  5. setFileAttachments(EmailFileAttachment[])
  6. setHtmlBody(String)
  7. setInReplyTo(String)
  8. setPlainTextBody(String)
  9. setOrgWideEmailAddressId(ID)
  10. setReferences(String)
  11. setSubject(String)
  12. setTargetObjectId(ID)
  13. setToAddresses(String[])
  14. setWhatId(ID)

Example Apex Class

Below class is a simple apex program to understand single email message. I  have created vf page with button. & when we click on that button that calls sending emails method in below class. Try this for sending emails

public class singleEmailExample {
	public PageReference sendingEmail() {
		Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
		String[] sendingTo = new String[]{''};
		String[] sendingToBccAdd = new String[]{''};
		String[] sendingTocAdd = new String[]{''};
		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;

Apex Page

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