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
ManidManid 

can any one explain about email services with example ?

i see messaging namespace and its methods and clases but i amnot understand properly
NagendraNagendra (Salesforce Developers) 
Hi Manid,

Email Services: Email services are automated processes that use Apex classes to process the contents, headers, and attachments of the inbound email. You can associate each email service with one or more Salesforce-generated email addresses to which users can send messages for processing.
 

Example of Email Service – Creating Contact from email
Presumption –

  • Subject should contain word “Create Contact”�
  • The body contains only Contact Name.

Apex Code with test method:
 

/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class CreateContactFrmEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

		String subToCompare = 'Create Contact';

		if(email.subject.equalsIgnoreCase(subToCompare))
		{
			Contact c = new Contact();
			c.LastName = email.plainTextBody;
			insert c;

                // Save attachments, if any
        	for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
          	Attachment attachment = new Attachment();

          	attachment.Name = tAttachment.fileName;
          	attachment.Body = Blob.valueOf(tAttachment.body);
          	attachment.ParentId = c.Id;
          	insert attachment;
        	}

        	//Save any Binary Attachment
        	for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
          	Attachment attachment = new Attachment();

          	attachment.Name = bAttachment.fileName;
          	attachment.Body = bAttachment.body;
          	attachment.ParentId = c.Id;
          	insert attachment;
        	}
		}

	result.success = true;
        return result;
    }

    static testMethod void testCreateContactFrmEmail() {
    	Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();

        email.subject = 'Create Contact';
        email.plainTextBody = 'FromEmail';
        env.fromAddress = 'ilovenagpur@gmail.com';

        CreateContactFrmEmail creatC = new CreateContactFrmEmail();
        creatC.handleInboundEmail(email, env );
    }
}

After creating the above Apex class, click Your Name | Setup | Develop | Email Services.

  • Click New Email Service to define a new email service.
  • Select above apex class, add the email address from where to accept the request and activate the service.
User-added imageAfter filling the form, click on “Save and New Email Address”�
User-added image

Note: The domain name in Email address must qualify the domain name entered at “email services”� in first step. For example : we have entered “gmail.com”� in first step that means it will accept the email address only from the gmail.com and that’s why in second step I have used email address displayed in screen shot.
After all the above steps, one email address is generated by the salesforce. Send the email to that address with subject line “Create Contact”� and contact name in email body.
In the above tutorial, I have used very simple example just to demonstrate that how the email services works in the salesforce.

Governance limit of Email services in salesforce:
Salesforce limits the total number of messages that all email services combined, including On-Demand Email-to-Case, can process daily. Messages that exceed this limit are bounced, discarded, or queued for processing the next day, depending on how you configure the failure response settings for each email service. Salesforce calculates the limit by multiplying the number of user licenses by 1,000, up to a daily maximum of 1,000,000.

Hope this helps.

Please mark this as solved if it's resolved so that it get removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Manid,
Email Services in Salesforce with a simple example.
/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class CreateContactFrmEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        String subToCompare = 'Create Contact';
        if(email.subject.equalsIgnoreCase(subToCompare))
        {
            Contact c = new Contact();
            c.LastName = email.plainTextBody;
            insert c;
                // Save attachments, if any
            for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
            Attachment attachment = new Attachment();
 
            attachment.Name = tAttachment.fileName;
            attachment.Body = Blob.valueOf(tAttachment.body);
            attachment.ParentId = c.Id;
            insert attachment;
            }
 
            //Save any Binary Attachment
            for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
            Attachment attachment = new Attachment();
            attachment.Name = bAttachment.fileName;
            attachment.Body = bAttachment.body;
            attachment.ParentId = c.Id;
            insert attachment;
            }
        }
    result.success = true;
        return result;
    }
    static testMethod void testCreateContactFrmEmail() {
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
        email.subject = 'Create Contact';
        email.plainTextBody = 'FromEmail';
        env.fromAddress = 'ilovenagpur@gmail.com';
        CreateContactFrmEmail creatC = new CreateContactFrmEmail();
        creatC.handleInboundEmail(email, env );
    }
}

Please refer the below link for reference.
http://www.jitendrazaa.com/blog/salesforce/email-services-in-salesforce-with-simple-example/

Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
ManidManid
thanks bro.