You need to sign in to do that
Don't have an account?
Apex email exception
Can anyone let me know where I'm going wrong? Below is my Apex class. I get the following error when the VF page tries to call it. I'm sure it's because it can't convert String to a List, but I have no idea how to fix it. The email examples given by Salesforce always have a hardcoded email address.Thank you for any help!
common.apex.runtime.impl.ExecutionException: Invalid conversion from runtime type String to List<String>
public with sharing class AttachmentUploadController
{
public String EmailSubject { get; set; }
public String EmailBody { get; set; }
public String ToEmails{ get; set; }
public Attachment attachment
{
get
{
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
public PageReference sendEmail()
{
String parentId = System.currentPagereference().getParameters().get('id');
String[] ToAddresses = this.ToEmails.split(';');
try
{
//Start: Send Email with Attachment
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(ToAddresses);
mail.setHTMLBody(this.EmailBody);
mail.setSubject(this.EmailSubject);
//Set email file attachments
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
// Add to attachment file list
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(attachment.Name);
efa.setBody(attachment.Body);
fileAttachments.add(efa);
//create attachment for object
mail.setFileAttachments(fileAttachments);
//Send email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
//END : Send Email with Attachment
PageReference page = new PageReference('/' + parentId);
return page;
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Email Not Sent'));
return null;
}
finally
{
attachment = new Attachment();
}
}
}
common.apex.runtime.impl.ExecutionException: Invalid conversion from runtime type String to List<String>
public with sharing class AttachmentUploadController
{
public String EmailSubject { get; set; }
public String EmailBody { get; set; }
public String ToEmails{ get; set; }
public Attachment attachment
{
get
{
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
public PageReference sendEmail()
{
String parentId = System.currentPagereference().getParameters().get('id');
String[] ToAddresses = this.ToEmails.split(';');
try
{
//Start: Send Email with Attachment
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(ToAddresses);
mail.setHTMLBody(this.EmailBody);
mail.setSubject(this.EmailSubject);
//Set email file attachments
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
// Add to attachment file list
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(attachment.Name);
efa.setBody(attachment.Body);
fileAttachments.add(efa);
//create attachment for object
mail.setFileAttachments(fileAttachments);
//Send email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
//END : Send Email with Attachment
PageReference page = new PageReference('/' + parentId);
return page;
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Email Not Sent'));
return null;
}
finally
{
attachment = new Attachment();
}
}
}
Your Code Looks good. its works for me also i am getting emails to my mail box. when i hard code the Email IDs. like below.
ToEmails='test@gmail.com ; xyz@gmail.com ; abc@gmail.com';
make sure you formating you ToEmails string in this fomate. print the string and check the ToEmails Values.
if possible post you VF page Code so that i can check it how your passing email id.
i hope it will help you.
Thanks
karthik