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
Marc VanderstraetenMarc Vanderstraeten 

Problem, Sending an Email in Apex

Hi there,
I'm using Salesforce.com for 2 months and I decided to learn Apex code to improve the efficiency of my programs. I've installed the Force.com IDE on Eclipse and everything was fine for my first program. In brief my second program is used to notify peoples about something. I've made a little test to see if sending emails works but it doesn't....

@isTest
private class MailTest {

    static testMethod void myUnitTest() {
       FNotifyContactChangedEmail.sendEmail();
    }
}


public class FNotifyContactChangedEmail{
    public static void sendEmail() {
        String[] toAddresses = new String[] {'exemple@exemple.ex','
exemple@exemple.ex'};
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Messaging.reserveSingleEmailCapacity(1);
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('
exemple@exemple.ex');
        mail.setSenderDisplayName('Salesforce Notifications');
        mail.setSubject('A contact has been modified ');
        mail.setPlainTextBody('Test');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}


That give me that error in the tab failures: Previous load of class failed: scout9|standardsheetlabelcontroller
And this in the log file :
|VARIABLE_SCOPE_BEGIN|[3]|toAddresses|LIST<String>|true|false
|VARIABLE_ASSIGNMENT|[3]|toAddresses|["exemple@exemple.ex","exemple@hotmail.ex"]|0x3095a4ae
|STATEMENT_EXECUTE|[4]
|FATAL_ERROR|Internal Salesforce.com Error
(1952023000)|CUMULATIVE_LIMIT_USAGE
[...]
Number of Email Invocations: 0 out of 10


Is there anything to configure in salesforce? Or something to implements in Eclipse ?

It would be great to have an answer to my question, I thank you in advance



Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

HI,

 

You have to simply move your class individually to the server using eclipse. Mail can’t be fired by just moving the test method to the server side. So to check the mail is being fired or not you have to simply login into the CRM as Admin. Now click on your name then you will find the option developer force console.

 

Click on this and here you have to simply type this FNotifyContactChangedEmail.sendEmail();And click on execute button. Now you can check that you have received that mail if your mail id is mentioned inside the toaddress.

 

Please move this code to the server using eclipse

public class FNotifyContactChangedEmail{

    public static void sendEmail() {

        String[] toAddresses = new String[] {'exemple@exemple.ex','exemple@exemple.ex'};

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        Messaging.reserveSingleEmailCapacity(1);

        mail.setToAddresses(toAddresses);

        mail.setReplyTo('exemple@exemple.ex');

        mail.setSenderDisplayName('Salesforce Notifications');

        mail.setSubject('A contact has been modified ');

        mail.setPlainTextBody('Test');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }

}

And follow the above  steps to check the email.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

All Answers

Navatar_DbSupNavatar_DbSup

HI,

 

You have to simply move your class individually to the server using eclipse. Mail can’t be fired by just moving the test method to the server side. So to check the mail is being fired or not you have to simply login into the CRM as Admin. Now click on your name then you will find the option developer force console.

 

Click on this and here you have to simply type this FNotifyContactChangedEmail.sendEmail();And click on execute button. Now you can check that you have received that mail if your mail id is mentioned inside the toaddress.

 

Please move this code to the server using eclipse

public class FNotifyContactChangedEmail{

    public static void sendEmail() {

        String[] toAddresses = new String[] {'exemple@exemple.ex','exemple@exemple.ex'};

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        Messaging.reserveSingleEmailCapacity(1);

        mail.setToAddresses(toAddresses);

        mail.setReplyTo('exemple@exemple.ex');

        mail.setSenderDisplayName('Salesforce Notifications');

        mail.setSubject('A contact has been modified ');

        mail.setPlainTextBody('Test');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }

}

And follow the above  steps to check the email.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

This was selected as the best answer
Marc VanderstraetenMarc Vanderstraeten

That's it! I thank you a thousand times, I could still look for days!

Anil DuttAnil Dutt

Hi Navatar,

 

I have following Apex class for sending email in eclipse

 

public class SendConfirmation {
    public final Contact con;
    public SendConfirmation(ApexPages.StandardController controller)
    {
        this.con=(Contact)controller.getRecord();
    }
    public void SendEmail()
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(con.Id);
        mail.setTemplateId('00Xd0000000PFaY');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

 

And this is the test case

 

@isTest
private class SendConfirmationTestCase {
    private static testMethod void myUnitTest() {
        Contact con =  new Contact();
        con.FirstName = 'Anil';
        con.LastName = 'Dutt';
        con.Email = 'anil@swiftsetup.com';
        insert con;
        
        SendConfirmation.SendMail();
    }
}

 

but getting following error

 

Save error: Method does not exist or incorrect signature: SendConfirmation.SendMail();

 

Please help to fix this

 

thanks



BialBial

Hello All.

I wrote trigger code for sending email to contacts in case, but now i want to write test code for that trigger in ecclipse. The following is my trigger.

 

trigger EmailDemoSendSingle on Case (after update, after insert) {
final String template = 'Test';
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();

Messaging.SingleEmailMessage message1 = new Messaging.SingleEmailMessage();

Messaging.SingleEmailMessage message2 = new Messaging.SingleEmailMessage();

Messaging.SingleEmailMessage message3 = new Messaging.SingleEmailMessage();

Messaging.SingleEmailMessage message4 = new Messaging.SingleEmailMessage();

 

message.setTemplateId([select id from EmailTemplate where Name =:template].id);

message1.setTemplateId([select id from EmailTemplate where Name =:template].id);

message2.setTemplateId([select id from EmailTemplate where Name =:template].id);

message3.setTemplateId([select id from EmailTemplate where Name =:template].id);

message4.setTemplateId([select id from EmailTemplate where Name =:template].id);

 

 

// Send single mail to contact of each updated case
for (Case uc: Trigger.new) {

Contact c = [select Email from Contact where Id =:uc.ContactId and Email!=null];

message.setTargetObjectId(c.Id);
message.setWhatId(uc.Id);

message.setToAddresses(new String[] {c.Email});
Messaging.sendEmail(new Messaging.Email[] {message});
}

for (Case vc: Trigger.new) {
IF (vc.Contactemail__c!=null){
Contact v = [select Email from Contact where Id =:vc.Contact__c and Email != null];

message1.setTargetObjectId(v.Id);
message1.setWhatId(vc.Id);

message1.setToAddresses(new String[] {v.Email});
Messaging.sendEmail(new Messaging.Email[] {message1});
}
IF (vc.Contact2email__c!=null) {
Contact b = [select Email from Contact where Id =:vc.Contact2__c and Email != null];

message2.setTargetObjectId(b.Id);
message2.setWhatId(vc.Id);

message2.setToAddresses(new String[] {b.Email});
Messaging.sendEmail(new Messaging.Email[] {message2});


}
IF (vc.Contact3email__c!=null) {
Contact d = [select Email from Contact where Id =:vc.Contact3__c and Email != null];

message3.setTargetObjectId(d.Id);
message3.setWhatId(vc.Id);

message3.setToAddresses(new String[] {d.Email});
Messaging.sendEmail(new Messaging.Email[] {message3});

}


IF (vc.Contact4email__c!=null) {
Contact f = [select Email from Contact where Id =:vc.Contact4__c and Email != null];

message4.setTargetObjectId(f.Id);
message4.setWhatId(vc.Id);

message4.setToAddresses(new String[] {f.Email});
Messaging.sendEmail(new Messaging.Email[] {message4});
}

else {
}

}
}

 

 

 

 

 

 

 

 

Thanks in advance.