30
votes
How to Test Messaging.sendEmail
You can use the Limits.getEmailInvocations method .
Test.startTest();
methodThatSendsEmail();
Integer invocations = Limits.getEmailInvocations();
Test.stopTest();
system.assertEquals(1, ...
13
votes
Accepted
How to Test Messaging.sendEmail
With respect to your edited question, as @AdrianLarson answered, your schedulable class still doesn't run until immediately after Test.stopTest(). Asserting the change in Limits.getEmailInvocations() ...
10
votes
How can I convert my htmlBody to plainTextBody using Apex/Regex
I think rather than using any RegEx, you can just use a built in String method: stripHtmlTags.
Salesforce String Class
So it would look like:
String plainText = htmlBody.stripHtmlTags();
9
votes
Salesforce Email Service Debugging
The likely answer comes from the comment by @MarcDBehr
Email Services can have several Email Addresses. Each Email Address has a context user (i.e. running user)
You need to enable debug logging for ...
9
votes
Issue Setting ContentVersion Owner
Add this line:
newContentVersion.firstPublishLocationId= user.Id;
A more detailed explanation:
When documents are created via apex (contentversion object), you need to specify the library otherwise ...
8
votes
Accepted
Do email results come in the same order as the list of emails
To answer the specific question, the sendEmail method returns a list of Messaging.SendEmailResult objects. This list will be in the same order as the corresponding list of emails provided to sendEmail(...
8
votes
Accepted
How to leverage org-wide email address within an email service?
2 options:
Option 1
1) Create a class which implements the inboundHandler.
global class VerifyOrgWideEmailAddress implements Messaging.InboundEmailHandler {
global Messaging....
7
votes
How to unzip a zipfile attachment in apex and process each file in the zip attachment
You can do this natively in Apex using the Zippex library which is open source.
https://github.com/pdalcol/Zippex
Your code would be similar to this:
Blob zipBlob; //This needs to contain the zip ...
7
votes
Accepted
Regex including curly brackets
Since backslashes are the escape character in Apex string literals, if you want a literal backlash, you need to escape it with a backslash.
So the right regex to use would be \\{.*\\}
6
votes
How to test if outbound email is send
In addition to the other answers, there is ApexMocks and the fflib Enterprise Pattern - specifically the Unit of Work Layer
Here's an example:
First, the class/method that sends the email:
public ...
6
votes
Accepted
custom Email Service - inline images appear as '[Image is no longer available]' upon Reply/ReplyAll
This is how I solved the problem, after saving off the attachments then I updated the message body to replace the cid references with URL to the files. For some magical reason, Salesforce will ...
6
votes
Accepted
Create a Salesforce file from an email attachment
Salesforce files have superseded attachments. A single attachment can have only one parent and access to attachments is controlled by the parent record. If we want to attach same file on two different ...
6
votes
Accepted
How to use an Apex Email Service to store emails on a record EXACTLY as Salesforce does?
From what I understand, You don't need to create a task to insert an EmailMessage. Email message has relatedToId field which you can use to directly link it to any desired record. As relatedToId is ...
6
votes
Accepted
Regex implementation in Salesforce: getting from known string to end of the line
You need to turn on multi-line mode in order for the $ specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m).
As Derek F. ...
5
votes
Apex: HTML email status to get open count
The EmailStatus object is not queryable directly (please vote for this idea), but it can be queried indirectly from Leads and Contacts as a child relationship sub-query as noted by Jenny Bennett on ...
5
votes
Accepted
Can an Apex Email Handler parse an attached Excel file?
Short Answer: No its not posssible to read Excel in Apex.
Long Answer: Excel files(xls,xlsx etc) are not a text based file. They are binary files. Thus unlike CSV(text based) they cant be read line ...
5
votes
Accepted
Inbound Email handler - Status being set as 'Sent' instead of 'New'
I think you can set the status value with the sequence number.
0 - New
1 - Read
2 - Replied
3 - Sent
4 - Forwarded
5 - Draft
So it could be like this
EmailMessage emailMessage = new EmailMessage();...
5
votes
Accepted
Apex SingleEmail using template and userId
This is expected behavior the User fields you are referencing are the sending user fields
User fields are also known as Sending User fields for administrators and users with the “Manage Users” ...
5
votes
Sending email functionality is not working for site guest user of org
Make sure your Guest User has a valid email assigned.
This is discussed in the article listed here.
4
votes
Accepted
How can I forward email service email to a different address?
What I have done in the past for an email service address is capture the email requesting verification and forward it to a real address so the link can be clicked to verify. Then you can use the long ...
4
votes
Email html body to plain text body
Use the String function stripHtmlTags() and it will do the work for you.
String1.stripHtmlTags()
4
votes
Accepted
How to know current Email governor limits?
You can use the Limits class to find out how many invocations remain as follows:
Integer remainingInvocations =
Limits.getLimitEmailInvocations() - Limits.getEmailInvocations();
4
votes
Accepted
Fail to send email by utility class
Remove this line
email.setSubject(template.name);
as error clearly says When a template is specified, the plain text body, html body, subject, charset, and "treatBodiesAsTemplate" may not be ...
4
votes
Accepted
Apex: HTML email status to get open count
You'll have to get creative, because the object you're looking for is EmailStatus, which is currently only available as a describeSObjects parameter. You can probably use Andrew Fawcett's Metadata ...
4
votes
Accepted
Send Email only if the field value changes
You can access the 'old' and the 'new values in an after trigger. You already access the new values in trigger.newMap. The old values are in trigger.oldMap :-)
So compare the old and new value of the ...
4
votes
Detect which email services address used by sender in Apex inbound email handler
One strategy is to use a different context user for each inbound email services address. Then, in the inbound email services handler, you can use UserInfo class to route logic accordingly
if (...
4
votes
Parse a email body for Salesforce Id
I've actually written something like this before. Here's a pared down version of what you're looking for:
public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, ...
4
votes
Issue Setting ContentVersion Owner
I found from document :
FirstPublishLocationId is id of the location where the version was first published. If the version is first published into a user's personal library or My Files, the field ...
4
votes
Accepted
Send email with attachments
06A is the key prefix for ContentDocumentLink, which points to the bug here:
system.debug(fileAttachments);
mail.setEntityAttachments(attach);
You are calling setEntityAttachments() ...
4
votes
Accepted
Insert Thread ID in the Email Subject option not visible on Email-to-Case
As far as I can see there is a new default for any org created after Winter'21 (which is the release being deployed by Salesforce).
Long story short: I think you cannot change it, if your org is ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
apex-email-service × 449apex × 227
email × 132
email-template × 50
singleemailmessage × 38
email2case × 34
visualforce × 29
inboundservices × 23
trigger × 20
email-alert × 18
massemailmessage × 15
unit-test × 13
class × 11
attachment × 11
emailmessage × 11
regular-expressions × 8
scheduled-apex × 7
html × 7
send-email × 7
batch × 6
process-builder × 6
case × 6
error × 6
pdf × 6
inboundemail × 6