0

I am trying to send an email with apex controller and VF interface. But when I send the message, I have an error :

System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, null: [] Error is in expression '{!sendPrivate}' in component in page sendemail: Class.sendEmail.sendPrivate: line 82, column 1

Class.sendEmail.sendPrivate: line 82, column 1

This is a part of my apex controller :

public class sendEmail {
public String subject { get; set; }
public String body { get; set; }
public blob docBody { get; set; }
public String docName { get; set; }
public List < Membres__c > membersP { get; set; }
public List < Membres__c > membersC { get; set; }
public List < Membres__c > membersCorp { get; set; }
public List < Id > idP { get; set; }
public List < Id > idC { get; set; }
public List < Id > idCorp { get; set; }

// Create a constructor that populates the Account object
public sendEmail() {

    List < Id > idP = new List < Id >();
    List < Id > idC = new List < Id >();
    List < Id > idCorp = new List < Id >();

    membersP = [select Id, Name, Contact__r.Id, Contact__c, Contact__r.Name, Contact__r.Email FROM Membres__c where RecordTypeId =: '012240000002GRv' AND Contact__r.Email != null LIMIT 1];
    membersC = [select Id, Name, Contact__r.Id, Contact__c, Contact__r.Name, Contact__r.Email FROM Membres__c where RecordTypeId =: '012240000002GSt' AND Contact__r.Email != null LIMIT 1];
    membersCorp = [select Id, Name, Contact__r.Id, Contact__c, Contact__r.Name, Contact__r.Email FROM Membres__c where RecordTypeId =: '01I24000001M8lz' AND Contact__r.Email != null LIMIT 1];

        idP.add('0032400000gKaFB');
    for (Membres__c m : membersP) {
        idP.add(m.Contact__c);
    }
    for (Membres__c m : membersC) {
        idC.add(m.Contact__c);
    }
    for (Membres__c m : membersCorp) {
        idCorp.add(m.Contact__c);
    }
}

public List < Membres__c > getMembresP() {
    return membersP;
}

public List < Membres__c > getMembresC() {
    return membersC;
}

public List < Membres__c > getMembresCorp() {
    return membersCorp;
}

// Partie Private
public PageReference sendPrivate() {

    // Création de l'email
    Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();

    // Create the email attachment
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName( docName );
    efa.setBody( docBody );

    // Gestion des paramètres généraux
    email.setTargetObjectIds( idP );
    email.setWhatIds( idP );
    email.setSubject( subject );
    email.setSenderDisplayName('URB-BKV');
    email.setTemplateID('00X24000000Mxfa');
    //email.setDescription( body );

    // Create email on contact
    email.setSaveAsActivity(true);

    // Attach document
    //email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

    // Send Email
    //Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.MassEmailMessage[] {email});
    //Messaging.sendEmail(new Messaging.Email[] { email });
    Messaging.sendEmail(new Messaging.MassEmailMessage[] { email });

    return null;
}

I do not have any Idea of the problem. The single email method works fine, but when i try the mass email constructor... Nothing Thanks for your help

Max

0

You are declaring same list in class level and constructor. So the scope of values added to the list will be within constructor. sendPrivate() is accessing values in idP list which is declared in class level which does not have any values. Ideally you should declare lists in class level and initialise in constructor. Try code below.

public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }
    public blob docBody { get; set; }
    public String docName { get; set; }
    public List < Membres__c > membersP { get; set; }
    public List < Membres__c > membersC { get; set; }
    public List < Membres__c > membersCorp { get; set; }
    public List < Id > idP { get; set; }
    public List < Id > idC { get; set; }
    public List < Id > idCorp { get; set; }

    // Create a constructor that populates the Account object
    public sendEmail() {

        idP = new List < Id >();
        idC = new List < Id >();
        idCorp = new List < Id >();

        membersP = [select Id, Name, Contact__r.Id, Contact__c, Contact__r.Name, Contact__r.Email FROM Membres__c where RecordTypeId =: '012240000002GRv' AND Contact__r.Email != null LIMIT 1];
        membersC = [select Id, Name, Contact__r.Id, Contact__c, Contact__r.Name, Contact__r.Email FROM Membres__c where RecordTypeId =: '012240000002GSt' AND Contact__r.Email != null LIMIT 1];
        membersCorp = [select Id, Name, Contact__r.Id, Contact__c, Contact__r.Name, Contact__r.Email FROM Membres__c where RecordTypeId =: '01I24000001M8lz' AND Contact__r.Email != null LIMIT 1];

        idP.add('0032400000gKaFB');
        for (Membres__c m : membersP) {
            idP.add(m.Contact__c);
        }
        for (Membres__c m : membersC) {
            idC.add(m.Contact__c);
        }
        for (Membres__c m : membersCorp) {
            idCorp.add(m.Contact__c);
        }
    }

    public List < Membres__c > getMembresP() {
        return membersP;
    }

    public List < Membres__c > getMembresC() {
        return membersC;
    }

    public List < Membres__c > getMembresCorp() {
        return membersCorp;
    }

    // Partie Private
    public PageReference sendPrivate() {

        // Création de l'email
        Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName( docName );
        efa.setBody( docBody );

        // Gestion des paramètres généraux
        email.setTargetObjectIds( idP );
        email.setWhatIds( idP );
        email.setSubject( subject );
        email.setSenderDisplayName('URB-BKV');
        email.setTemplateID('00X24000000Mxfa');
        //email.setDescription( body );

        // Create email on contact
        email.setSaveAsActivity(true);

        // Attach document
        //email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        // Send Email
        //Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.MassEmailMessage[] {email});
        //Messaging.sendEmail(new Messaging.Email[] { email });
        Messaging.sendEmail(new Messaging.MassEmailMessage[] { email });

        return null;
    }
}

Points to note:

  • Declare variables in class level if same variables are used in different methods.
  • Avoid hard coded ids
  • Minimal number of SOQL. If you are querying same object the best practise is to have one soql.
  • Hi Reshma, Thanks, it works. The hard coded IDs was to test it on one specific email. I will delete it. I can have one SOQL, and I will, thanks for advices. I don't know what I did with this lists... I have additional questions just to be sure. We have to use template for mass email ? Could we not just use a VF page with rich text to set the body ? (Maybe I have to create/edit a template with VF to use it...) And can we not log activities on Contact with standard method ? And the last question : Can I add an attachment in mass email ? Thanks again for your advices. – Max Feb 27 '17 at 23:12
  • @Max Glad to help you. Can you post your new queries as new question. – Reshma Feb 28 '17 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.