TransWikia.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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP