AnswerBun.com

Single Email message with attachment not sending email

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

One Answer

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

Add your own answers!

Related Questions

Opportunity is not updated on contact detail page

1  Asked on October 3, 2021 by shalini-goyal

       

Indexing software used in salesforce

2  Asked on February 28, 2021

   

Multi step Approval Process

0  Asked on February 27, 2021 by sai-vineeth

 

Convert User ID to Name with AMPScript

1  Asked on February 22, 2021 by greg-noble

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP