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
Vasundhara VaishnavVasundhara Vaishnav 

Has anyone post to chatter by sending an email through apex code?

I have read that with Spring 14, Email to Post to Chatter Groups is Generally Available. Users can post to groups by sending emails from the email address associated with their Salesforce account. Please refer below link for more information:

https://help.salesforce.com/apex/HTViewSolution?urlname=How-to-allow-users-to-post-to-Chatter-Groups-via-Email&language=en_US

I have configured it for my organization and I am able to post to chatter group by sending an email from my mailbox directly.

Now, I am trying to post to chatter by sending an email through apex code. And here problem comes. No error is occurring but Content is also not getting posted to chatter group.

Below is my apex code:

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
    String emailSubject = 'Test email subject';
    String[] toAddresses = new String[] {[toemailaddress]}; 
    mail.setToAddresses(toAddresses); 
    mail.setBccSender(true);
    mail.setSubject(emailSubject);
    mail.setSenderDisplayName('License');
    String mailBody = 'Mail sent from apex class';

    mail.setPlainTextBody(mailBody);   
    mail.setSaveAsActivity(false);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});

In the above code, If I replace [toemailaddress] to some gmail/yahoo, or other valid email address, then mail is getting sent. But If I replace [toemailaddress] to my chatter group email address which is something like this "0f990000000llhpcak@post.9-vxv6eam.ap1.chatter.salesforce.com", then mail is not getting sent.

Can anyone has any idea how can I achieve this through apex code?

My main requirement is that I always want to post my content in production's chatter no matter I am on INT, QA or UAT. That's why I have chosen "Post By Email" feature which is released in Spring 14. But unfortunately, I am not able to achieve the same through apex code. 
NagaNaga (Salesforce Developers) 
Hi Vasundhara,

You dont need an apex code to achieve this functionality/Email-to-case, Visual workflow and Flow Trigger might serve your purpose. We need  not use a single line of apex code.  It’s very easy to implement, any Salesforce admin also can implement this for their organization. I will first configure email-to-case then we can use Visual workflow and Flow Trigger to create Chatter Post. Before proceeding you have to understand FeedItem objects in Salesforce.  

Please see the link below

https://rakeshistom.wordpress.com/2014/05/21/post-to-chatter-by-sending-an-email/

Best Regards
Naga Kiran
Vasundhara VaishnavVasundhara Vaishnav
Hi Naga,

Thanks for your answer. By following your links, I can able to Post to Chatter in same org. But my main requirement is that I always want to post my content in production's chatter no matter I am on INT, QA or UAT. And this should be done in schedulable class. That'w why I want to achieve this functionality through apex code. Is it possible to achieve the same with apex code?

Thanks in advance!
brave-moosebrave-moose
Hi, is there any solution for this issue? I want to send email to Chatter Group's email address via apex code that Vasundhara mentioned above and I'm  facing the same problem. If I replace [toemailaddress] to some valid email address, then mail is getting sent, but if I replace [toemailaddress] to my chatter group email address, then mail is not getting sent