0

im using this code to sending mail but no mail was send.What is wrong Thanks in advance.

   List<Result> results = new List<Result>();

   for(ResultWrapper wrapper: lstResultWrapper)
   {
       if(wrapper.isSelected == true)
       {
           results.add(wrapper.objResult);
       }
   }

   EmailTemplate templateId = [Select id from EmailTemplate where name = 'Contact: Follow Up (SAMPLE)'];    
   List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   mail.setTemplateID(templateId.Id); 
   mail.setSaveAsActivity(false);

   List<String> toAddress= new List<String>();
   for(Result r: results)
   {
        toAddress.add(r.ins);
   }

   mail.setToAddresses(toAddress);
   allmsg.add(mail);        
   Messaging.sendEmail(allmsg,false);

I am trying to send email to external users

  • Messaging.sendEmail(allmsg,false); instead use Messaging.sendEmail(allmsg);.. If you use like this then you will get error. After that update your question with error. – Ratan Paul Jan 15 '16 at 8:52
  • 1
    you need to use targetObjectId.. If you want use setTemplateID then you have to use targetObjectId. – Ratan Paul Jan 15 '16 at 9:05
  • @Ratan what is targetObjectId – moin khan Jan 15 '16 at 10:26
1

If you want to use EmailTemplate then you have to use setTemplateID.

setTargetObjectId(targetObjectId)

Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.


In you case you are trying to send email to external user..

So you need to use setHtmlBody or setplainTextBOdy methods for email body.

   List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   mail.setSaveAsActivity(false);
   mail.setSubject('stackforce answer');
   mail.setplainTextBOdy('Test');
   List<String> toAddress= new List<String>();
   toAddress.add('test@gmail.com');
   mail.setToAddresses(toAddress);
   allmsg.add(mail);        
   Messaging.sendEmail(allmsg);

using setHtmlBody

Example

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'foo@bar.com'});
mail.setSubject('stackforce answer');
String messageBody = '<html><body>Hi, welcome to &nbsp;&nbsp;&nbsp;Our World</body>  </html>';
mail.setHtmlBody(messageBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
0

Try using

Messaging.sendEmail(allmsg);

instead of Messaging.sendEmail(allmsg,false); at least you will know what error you are getting.

  • 1
    This is not an ans to the question. if you want ask anything ask in comment – Ratan Paul Jan 15 '16 at 8:56

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.