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

Help with send email method

I have a method intended to generate an email with a users login credentials with a button click.  I used to have it pulling the information off a custom object but am now trying to consolidate with just the standard Contact object, but now in my tests it throws an error in execution.  My code is:
public with sharing class CM_InfoController{

        private static String ORG_WIDE_EMAIL_ADDRESS = '';
        public Contact user    {get;set;}
        private static ID orgWideEmailId = [SELECT id FROM OrgWideEmailAddress WHERE address=:ORG_WIDE_EMAIL_ADDRESS].id;
    public CM_InfoController(){
    public CM_InfoController(ApexPages.StandardController controller){
       user = (Contact)controller.getRecord();
    public PageReference sendEmail(){
        PageReference pageReference = null;
        if(user != null){
                Contact nu = [SELECT Id, Name, Password__c, Email FROM Contact WHERE];
                if(orgWideEmailId != null){
                  // Build new URL
                  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                  String htmlBody = '<h1>MAG User Info Request</h1>' +
                  '<p>Dear ' + nu.Name + ',</p>' +
                  '<p>We have received your request for user credential information in order to access some restricted features of the Maximus Auto Group website.</p>' +
                  '<p>Your Username is: ' + nu.Email + '</p>' +
                  '<p>Your Password is: ' + nu.Password__c + '</p>' +
                  '<p>If you have any questions or concerns regarding this information, please contact one of our associates at 1-800-801-1342 and we will assist you, or reply to this email with any specific questions.</p>' +
                  '<p>Thank you for your business and continued support!</p>' +
                  '<p>MAXIMUS AUTO GROUP</p>';
                   //Build Email
                            String[] toAddresses = new String[]{nu.Email};                                              
                                            mail.setSubject('MAG User Access');
                                        Messaging.sendEmail(new Messaging.Email[]{mail},true);
                            ApexPages.Message msg = new ApexPages.Message( ApexPages.Severity.INFO, 'Message sent successfully');                  
                            ApexPages.Message msg = new ApexPages.Message( ApexPages.Severity.ERROR, 'Org wide email address not found ['+ ORG_WIDE_EMAIL_ADDRESS + ']');                  
                                ApexPages.Message msg = new ApexPages.Message( ApexPages.Severity.ERROR, 'Email not specified on user to send information.');                  

And the error I encounter each time is: "First exception on row 0; first error: INVALID_ID_FIELD, Only accounts, assets, campaigns, cases, contracts, opportunities, orders, products, solutions and custom objects are allowed as whatId.: "  which is directed at the "mail.setWhatId(;" line.  However, this was working properly with the custom object I used to have in place so I am unsure why this would suddenly not work when all I did to the code was change the object referenses from my custom one to the Contact one.
Best Answer chosen by Carter85
You dont need to setWhatId in your email as you are not using any merge fields. Try removing/commenting the mail.setWhatId(

All Answers

You dont need to setWhatId in your email as you are not using any merge fields. Try removing/commenting the mail.setWhatId(
This was selected as the best answer
That did the trick, thanks.