Salesforce Asked by prasad nagamalli on December 17, 2020
I want to send a email with attachment, for that using below code
public Attachment attachment
{
get
{
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}
public PageReference save() {
String parentId = System.currentPagereference().getParameters().get('pid');
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = parentId;
attachment.IsPrivate = false;
try
{
insert attachment;
//Start: Send Email with Attachment
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{UserInfo.getUserEmail()};
mail.setToAddresses(toAddresses);
mail.setSubject('subject1');
mail.setHtmlBody('Here is the email you requested: '+attachment.Body);
//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);
Attachment att = new Attachment(name = attachment.name, body = attachment.body, parentid = attachment.ParentId);
mail.setFileAttachments(fileAttachments);
//Send email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
PageReference page = new PageReference('/' + parentId);
return page;
}
catch (Dmlexception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'Error: Unable to upload the attachment.'));
return null;
}
finally
{
attachment = new Attachment();
}
}
It is not throwing any error but email is not sending, Please help me out guys
in order to be able to send single email messages, you need to have Access level All email in Deliverability for your organization
Answered by Oleksandr Berehovskyi on December 17, 2020
2 Asked on October 3, 2021 by santosh
2 Asked on October 3, 2021 by anton-solomin
1 Asked on October 3, 2021 by gershon
1 Asked on October 3, 2021 by felix-drexler
cloudpage custom link journeybuilder marketing cloud unsubscribe
2 Asked on October 3, 2021 by willem-ruys
1 Asked on October 3, 2021 by shalini-goyal
1 Asked on October 3, 2021 by dan-beer
0 Asked on March 6, 2021
1 Asked on March 1, 2021
3 Asked on February 26, 2021 by sarvesh
0 Asked on February 25, 2021 by return0
0 Asked on February 24, 2021 by sfdev
apex getcontentaspdf pageblocksection pageblocktable visualforce
0 Asked on February 24, 2021 by jagular
0 Asked on February 24, 2021 by user6670
3 Asked on February 23, 2021 by d-horse
0 Asked on February 23, 2021 by anas-rashid
0 Asked on February 22, 2021 by sf1dev
1 Asked on February 22, 2021 by greg-noble
Get help from others!
Recent Answers
© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP