function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Inbox OutboxInbox Outbox 

Greetings!

I need help in writing a code to send email both single and mass email with LWC and apex both? Please correct me and advice me. 
I want to be able to create a custom component where we can attach a file (template) and send it. 
I also want to write a trigger with/without a helper class to be able to send a mass email. 
Any use case is fine, please mention the use case so that my silly brain can co-relate.
 
Best Answer chosen by Inbox Outbox
PriyaPriya (Salesforce Developers) 

Hi 

Please refer this article which will help you to achieve your requirement :- 

https://github.com/MrRajatSharma/Lightning-web-component-send-email

https://webkul.com/blog/sending-email-apex/

https://developer.salesforce.com/forums/?id=9060G000000I7kUQAS

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm

Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan

 

All Answers

PriyaPriya (Salesforce Developers) 

Hi 

Please refer this article which will help you to achieve your requirement :- 

https://github.com/MrRajatSharma/Lightning-web-component-send-email

https://webkul.com/blog/sending-email-apex/

https://developer.salesforce.com/forums/?id=9060G000000I7kUQAS

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm

Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan

 

This was selected as the best answer
Inbox OutboxInbox Outbox
Thank you Priya for advising me. 
Inbox OutboxInbox Outbox
I am trying to send a mass welcome email (new customer template) to contacts after we save the contact. It should be a singleEmail instead of massEmail but I am okay with that. All I want is to know why I am getting the error. 
I have the following code with an error. Please advice!

class:
public class massEmail 

{
    public void mail(){
        list<contact> l= [SELECT ID FROM contact LIMIT 2];
        list<ID> I= new list<ID>();
        for(contact c: l){
            I.add(C.Id);    
        }
        
        EmailTemplate e = [SELECT ID FROM EmailTemplate WHERE Name = 'Sales: New Customer Email'];
        messaging.MassEmailMessage  m = new messaging.MassEmailMessage();
        m.setTargetObjectIds(I);
        m.setSenderDisplayName('System Admin');
        m.setTemplateId(e.Id);
        messaging.sendEmail(new messaging.MassEmailMessage [] {m});     
    }
}

Trigger:
trigger NewCustEmail on Contact (after insert)

{
    if(trigger.IsAfter){
         massEmail.mail(trigger.new);
    }
}

Error
Method does not exist or incorrect signature: void email(List<Contact>) from the type massEmail


 
Suraj Tripathi 47Suraj Tripathi 47

Hi ,

 

1. You need to make your method static like 

public static void mail(List<Contact> conList){


2.You are not passing the argument in your method "mail()" but you are sending an argument from trigger "massEmail.mail(trigger.new);" . It is mandatory to pass argument in the method.

public static void mail(List<Contact> conList){}

so you can correct your whole code like this:

public class massEmail {
    public static void mail(List<Contact> conList){
        list<contact> l= [SELECT ID FROM contact where id in:  conList];
        list<ID> I= new list<ID>();
        for(contact c: l){
            I.add(C.Id);    
        }
        EmailTemplate e = [SELECT ID FROM EmailTemplate WHERE Name = 'Sales: New Customer Email'];
        messaging.MassEmailMessage  m = new messaging.MassEmailMessage();
        m.setTargetObjectIds(I);
        m.setSenderDisplayName('System Admin');
        m.setTemplateId(e.Id);
        messaging.sendEmail(new messaging.MassEmailMessage [] {m});     
    }
}

Please mark it as Best Answer
Thank You