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
ThomasmThomasm 

apex class for inboud and outbound email service

I am trying to get my class to pass but i keep getting an error when i run it.  there error is
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : null: []

 here is the test class

@isTest
public Class TestEamilAttchment{
   Static testMethod Void TestinBoundEmail()
       {
   //create a new email and envelope ojbect
   messaging.InboundEmail email = new Messaging.InboundEmail();
   Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
   
   //setup the data for the email
   email.subject='Create Service Call Log';
   env.fromAddress='Tmonson@cms4.com';
   email.plainTextBody='Email body\n2332345354454\nTitle';
   email.FromName ='Thomas Monson';
   
        Service_Call_Log__C sService_Call_Log = new Service_Call_Log__C();   
   sService_call_log.Contact__C='00330000012mk3O';
 
   
   //add an Binary attachment
   
   Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
   attachment.body = blob.valueOf('My attachmeent text');
   attachment.fileName = 'textfileone.txt';
   attachment.mimeTypeSubType='Text/Plain';
   email.binaryAttachments=new Messaging.inboundEmail.BinaryAttachment[]{attachment};

   
   
   //add an Text attachmet
   
   Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
   attachmenttext.body = 'my attachment text';
   attachmenttext.fileName = 'textfiletwo3.txt';
   attachmenttext.mimeTypeSubType='texttwo/plain';
   email.textAttachments = new Messaging.inboundEmail.TextAttachment[]{attachmenttext};  
      //call the email serivce class and text it with the data in the textMethod
   
  EmailToServiceCallLog testInbound= new EmailToServiceCallLog ();
    testInbound.handleInboundEmail(email,env);          
      }
      }

Best Answer chosen by Thomasm
ThomasmThomasm

in case someone else has this issue the fix was

 

change

 env.fromAddress='Tm@.com' (mailto:env.fromAddress='Tm@.com');

to email.fromaddress

All Answers

ThomasmThomasm

here is the other class

global class EmailToServiceCallLog implements Messaging.InboundEmailHandler {
         global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
            Messaging.InboundEnvelope envelope) {        
            Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
            Service_Call_Log__C sService_Call_Log = new Service_Call_Log__C();  
            Try{
           Contact vCon=[SELECT ID, Name, Phone, Email FROM Contact WHERE email = :email.fromAddress LIMIT 1];       
            sService_Call_Log.Contact__C=vCon.id;
            sService_Call_Log.Contact_Phone_Number__C=vCon.Phone;
            }
            Catch(QueryException e){
            system.debug('Query Issue: '+e);
            }       

            sService_Call_Log.Job__C = 'a013000000UHggN';
            sService_Call_Log.Subject__C = email.subject;
            sService_Call_Log.description__C= email.plainTextBody;
            sService_Call_Log.Web_Email__C=email.FromAddress;
            sService_Call_Log.Web_name__C=email.FromName;
            insert sService_Call_log;  
           
                    
             if (email.textAttachments != null)
            {
            for(Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments)
            {
            Attachment attachment = new Attachment();
            attachment.Name = tAttachment.fileName;
            attachment.Body = Blob.valueOf(tAttachment.body);
            attachment.ParentId = sService_Call_log.Id;
            insert attachment;
            }
            }
            //Save any Binary Attachment
            if (email.binaryAttachments != null)
            {
            for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
            Attachment attachment = new Attachment();
            attachment.Name = bAttachment.fileName;
            attachment.Body = bAttachment.body;
            attachment.ParentId = sService_Call_log.ID;
            insert attachment;
            }
           }
           Messaging.SingleEmailMessage Mail = New Messaging.SingleEmailMessage();
           //mail.setTemplateID('00Xc0000000MMPC');
           mail.setToaddresses(new String[] {sService_Call_log.Web_email__C});
           mail.setReplyTo('ITHelp@cms4.com' (mailto:'ITHelp@cms4.com'));
           mail.setSubject('Your Case Has been Created');
           mail.setPlainTextBody('Dear Customer, \r\n \r\n Your service request regarding "'+ sService_call_log.subject__C + '" has been created and your assigned LaCosta Representative has been notified.  The details of your request "' + sService_call_log.description__c + '" have been communicated. \r\n \r\n We are committed to acting quickly and efficiently on your service request.  You will be notified when you request has been fullfiled. Our Target is to fulfill your service request in 24 hours or less.  If your service request is not fulfilled within 24 hours you will be notified by a customer serivice representative. \r\n \r\n We appreciate you communicating this service request to us.   \r\n \r\n Thank You,  \r\n \r\n LaCosta Support');
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
              
        result.success = true;
        return result;            
   }
}

ThomasmThomasm

in case someone else has this issue the fix was

 

change

 env.fromAddress='Tm@.com' (mailto:env.fromAddress='Tm@.com');

to email.fromaddress

This was selected as the best answer