There was a similar question asked here, but it wasn't really answered.
I've got a function that sends an e-mail for the User from a Visualforce Page, pretty simple:
Visualforce Page
<apex:pageMessages id="emailresponse" />
<apex:form >
<div class="form-group">
<apex:outputLabel for="to" value="To:" />
<apex:inputText id="to" styleClass="form-control" value="{!to}" />
</div>
<div class="form-group">
<apex:outputLabel for="subject" value="Subject:" />
<apex:inputText id="subject" styleClass="form-control" value="{!subject}" />
</div>
<div class="form-group">
<apex:outputLabel for="message" value="Message:" />
<apex:inputTextarea id="message" styleClass="form-control" value="{!message}" />
</div>
<apex:commandButton styleClass="btn btn-default" value="Send" action="{!sendEmail}" reRender="emailresponse" />
</apex:form>
This just takes in 3 parameters: to
, subject
and message
and calls the function sendEmail
in the Controller.
With all of the variable assignments, this is what the Controller looks like:
Controller:
private List<String> toList {
get;
set;
}
public String to {
get;
set;
}
private void createToList(String to) {
toList = new List<String>();
this.toList = to.split('\\;');
}
public String subject {
get;
set;
}
public String message {
get;
set;
}
public List<Messaging.SingleEmailMessage> messageList {
get;
set;
}
public PageReference sendEmail() {
User usr = [SELECT Id, FirstName, LastName, Name, Title, Email, Phone FROM User WHERE Id = :UserInfo.getUserId()];
createToList(to);
messageList = new List<Messaging.SingleEmailMessage>();
messageList = EmailHelper.createPersonalMessage(toList, subject, message, usr, false);
try {
Messaging.SendEmailResult[] r = Messaging.sendEmail(messageList);
if (r.get(0).isSuccess()) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, messageList.size() + ' messages sent.'));
}
}
catch(Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'There was a problem sending your messages.'));
System.debug('@@@@@ Error: ' + e);
}
return null;
}
As you can see this is pretty simple, it's just assigning the variables (albeit splitting e-mail addresses if they're comma-seperated), calling a function in EmailHelper
called CreateMessage
to create a list of single e-mail messages and trying to send it!
Finally, the Helper class which puts it all together:
Helper Class
public static List<Messaging.SingleEmailMessage> createPersonalMessage(List<String> toList, String subjectString, String messageString, User fromUser, Boolean doRemoveUnsubscribed) {
List<Messaging.SingleEmailMessage> messageList = new List<Messaging.SingleEmailMessage>(); // Create the new message list
String footer = buildFooter(fromUser); // Build the footer
Map<String, Id> mapEmailToContactId = new Map<String, Id>(); // Create a new map for the TargetObjectId
for (Contact c : [SELECT Id, Email FROM Contact WHERE Email IN :toList]) { // Loop through the list, mapping the e-mail address to the contact
mapEmailToContactId.put(c.Email, c.Id); // Add them
}
for (String to : toList) { // Loop through the Call List
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); // Create a new message
message.setToAddresses(new List<String>{
to
}); // Set the to address...
message.setSubject(subjectString); // ... Add the subject...
message.setHtmlBody(messageString + footer); // ... The body plus the footer we built earlier...
if (mapEmailToContactId.containsKey(to)) {
message.setTargetObjectId(mapEmailToContactId.get(to)); // ... The target object (if it exists)...
}
messageList.add(message); // ... Add it!
}
messageList = removeDuplicates(messageList); // Remove duplicates
if (doRemoveUnsubscribed) { // Do we remove Unsubscribed people?
messageList = removeUnsubscribed(messageList); // If so, remove them
}
return messageList; // Send the kust back
}
public static String buildFooter(User fromUser) {
return '<br><br><b><span style="color:#767171; font-family:arial,sans-serif">' + fromUser.FirstName + ' ' + fromUser.LastName + '</span>'; // This gets ridiculously long, but it's creating a footer for the e-mails
}
As mentioned in the title, I'm getting this error:
Not Serializable: com/salesforce/api/fast/List$$lcom/salesforce/api/Messaging/SingleEmailMessage$$r
I've tried Exchanging Messaging.SendEmailResult[] r = Messaging.sendEmail(messageList);
for just Messaging.sendEmail(messageList);
but still get the crash, sending e-mails with and without multiple address, comma-seperated. They work whether I enter 1 or a dozen thinking it maybe something to do with one of those two things.
I'm assuming $$r
is somehow in reference to the Controller line: Messaging.SendEmailResult[] r
, what's odd is that the e-mails do actually send.
So, what is causing this error and what does the error actually mean?
Messaging.SendEmailResult[] r = Messaging.sendEmail(messageList);
is working and that the helper class is doing it's business at least. I'll add some debugging in to see if I can trace the problem.