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
Varun99Varun99 

test coverage

Hi,

how to write a test class single email message

 

public class emailclass1
{
public String error{get;set;}
public String successMsg{get;set;}
public List<String> addresses {get;set;}
public boolean flagToSendMail;
public string plainbody{get;set;}
public string Email{get;set;}
public string Cc{get;set;}
public string BCc{get;set;}
public string subject{get;set;}
public attachment attach{get;set;}
public string Richtext{get;set;}
public emailclass1()
{
system.debug('^^^^^^'+attach);
attach=new attachment();
}
public String message {get;set;}


public pagereference send()
{
if(Email==null|| Email=='' || attach==null)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please check your uasername and email id');
ApexPages.addMessage(myMsg);
return null;
}
else
{

if(Email != '' || cc != '' || bcc != '' || attach!=null)
{
List<String> toAddresses = new List<String>();
List<String> ccAddresses = new List<String>();
List<String> bccAddresses = new List<String>();

List<String> tmpAddr = new List<String>();
if(Email.contains(','))
{
toAddresses = Email.split(',');
for(String tmp:toAddresses)
{
tmpAddr.add(tmp);
}
}
else if(Email != '')
{
toAddresses.add(Email );
tmpAddr.add(Email);
}
if(cc.contains(','))
{
ccAddresses = cc.split(',');
for(String tmp:ccAddresses)
{
tmpAddr.add(tmp);
}
}
else if(cc != '')
{
ccAddresses.add(cc);
tmpAddr.add(cc);
}
if(bcc.contains(','))
{
bccAddresses = bcc.split(',');
for(String tmp:bccAddresses)
{
tmpAddr.add(tmp);
}
}
else if(bcc != '')
{
bccAddresses.add(bcc);
tmpAddr.add(bcc);
}

addresses = tmpAddr;
for(String tempStr:addresses)
{
if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', tempStr))
{
error = 'Check To, CC and BCC addresses';
successMsg = '';
flagToSendMail = false;
break;
}
else
{
flagToSendMail = true;
}
}


if(flagToSendMail == true)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if(toAddresses != null)
{
mail.setToAddresses(toAddresses);
}
if(toAddresses != null)
{
mail.setccAddresses(ccAddresses);
}
if(toAddresses != null)
{
mail.setbccAddresses(bccAddresses);
}
Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[]{};
Messaging.EmailFileAttachment pdfFileAttachment = new Messaging.EmailFileAttachment();
pdfFileAttachment.setBody(attach.body);
pdfFileAttachment.setFileName('file xlsx');
fileAttachments.add(pdfFileAttachment);


mail.setToAddresses(toAddresses);
mail.setCcAddresses(CcAddresses );
mail.setBCcAddresses(BCcAddresses );
mail.setSubject(subject);
mail.setPlainTextBody(plainbody);
//mail.setFileAttachments(new Messaging.EmailFileAttachment{attach});
mail.setFileAttachments(fileAttachments );

Messaging.SendEmailResult[] sendResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});


}
}


//ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Email sent successfully');
//ApexPages.addMessage(myMsg);
Email='';
subject='';
plainbody='';
cc='';
bcc='';

 

 

}
return null;
}


}

 

 

 

My test class

 

@istest
private class emailtestclass
{
static testmethod void test()
{


Attachment attach=new Attachment();



Messaging.EmailFileAttachment pdfFileAttachment = new Messaging.EmailFileAttachment();
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
mail1.setPlainTextBody('plainbody');
emailclass1 mail=new emailclass1();

pdfFileAttachment.setBody(attach.body);
pdfFileAttachment.setFileName('file xlsx');


mail.Email='suresh@gmail.com,suresh.a@gmail.com';
mail.cc='suresh.p@gmail.com,suresh.ar@gmail.com';
mail.bcc='suresh@gmail.com,suresh.ad@gmail.com';
mail.send();

mail.Email='suresh.a@gmail.com';
mail.cc='';
mail.bcc='';
mail.send();

}
static testmethod void test1()
{


}
}

 

 

 

i got an error like

 

System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing body, need at least one of html or plainText: []Class.emailclass1.send: line 129, column 1 Class.emailtestclass.test: line 24, column 1

 

 

Please help me

 

DuTomDuTom

syntax...  in UT you are setting the textBody in 'mail1'  but sending 'mail'

Varun99Varun99

Hi tom,

 

Thnks for your reply. Can you please explain brief. how to pass plaintextbody in test class

 

 

Thank you

ManjunathManjunath

Hi!!!

 

In your test class set the body to the email before calling " Emal.send()" ,something like this.

 

 

mail.Email='suresh@gmail.com,suresh.a@gmail.com';
mail.cc='suresh.p@gmail.com,suresh.ar@gmail.com';
mail.bcc='suresh@gmail.com,suresh.ad@gmail.com';

mail.setPlainTextBody('plainbody');    // or something like this --  mail.plainbody=''Hello there;

mail.send();

With Regards,

Manjunath 

Varun99Varun99

Hi manjunath,

 

Thanks for your reply. Exactly you are right But my problem is am getting error on attachment

in email message am not inserting my attachment in any sobject directly sending to emails

like gmail interface I am facing Below error...

 

 System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body specified in the file attachment: []

 

In my Class attachment

 

Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[]{};
Messaging.EmailFileAttachment pdfFileAttachment = new Messaging.EmailFileAttachment();
pdfFileAttachment.setBody(attach.body);
pdfFileAttachment.setFileName('file xlsx');
fileAttachments.add(pdfFileAttachment);


mail.setToAddresses(toAddresses);
mail.setCcAddresses(CcAddresses );
mail.setBCcAddresses(BCcAddresses );
mail.setSubject(subject);     
mail.setplaintextBody(plainbody);
//mail.setFileAttachments(new Messaging.EmailFileAttachment{attach});
mail.setFileAttachments(fileAttachments );

Messaging.SendEmailResult[] sendResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});




Thank you
ManjunathManjunath

Hi Varun,

 

I feel you should set the attachment  property defined in your cls before sending. 

 

1. Create an instance of attachment, Set its all required field.

2. Then assign this to attachment property in your class.

 

This should be done before the mail.send() method call.

 

With regards,

Manjunath

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.