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
Praveen NPraveen N 

Not able send email from apex code

Hi All,

 

From apex i am sending single email message. it is not showing any errors and exceptions. but still mail is not going.

code as below.

Opportunity oppty=[select id,ownerID,owner.email from Opportunity where id = :OpptyId];
EmailTemplate OwnerChangeET= [SELECT id FROM EmailTemplate WHERE developerName = 'OpportunityChangeOwner'];

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
String[] toAddresses = new String[] {oppty.owner.email} ;
mail.setToAddresses(toAddresses) ;
mail.setTargetObjectId(oppty.ownerID);
mail.setWhatId(oppty.Id);
//mail.saveAsActivity = false;
mail.setSaveAsActivity(false);
mail.setTemplateId(OwnerChangeET.id);
List<Messaging.SendEmailResult> results = new list<Messaging.SendEmailResult>();
List<Messaging.SingleEmailMessage> singleEmailList= new list<Messaging.SingleEmailMessage>();
singleEmailList.add(mail);
results=Messaging.sendEmail(singleEmailList);
if (results[0].isSuccess())
System.debug('&&&& ' +results[0]);

 

in the debug i am getting success .  i have tried with with sharing and without sharing class.

 

debug:

01:26:19.829 (829047000)|SYSTEM_CONSTRUCTOR_EXIT|[38]|<init>()
01:26:19.829 (829066000)|SYSTEM_METHOD_ENTRY|[39]|LIST<Messaging.SingleEmailMessage>.add(Object)
01:26:19.829 (829088000)|SYSTEM_METHOD_EXIT|[39]|LIST<Messaging.SingleEmailMessage>.add(Object)
01:26:19.829 (829140000)|SYSTEM_METHOD_ENTRY|[40]|Messaging.sendEmail(LIST<Messaging.Email>)
01:26:19.928 (928161000)|EMAIL_QUEUE|[40]|subject: A new Opportunity has been Assigned to you, bccSender: false, saveAsActivity: false, useSignature: true, toAddresses: [praveenkumar.reddy@hp.com], targetObjectId: 005d0000000f3aSAAQ, whatId: 006Z0000003rmqcIAA, templateId: 00XG0000001eql8MAA, htmlBody: <html>
<p>Hi PraveenKumar Reddy N,</p>

<p>You have been assigned ownership of the Opportunity: TestOpportunity</p>
<p>
For more details, click the following link:</p><a href="https://hp--supportdev--c.cs11.visual.force.com/006Z0000003rmqcIAA">https://hp--supportdev--c.cs11.visual.force.com/006Z0000003rmqcIAA</a></html>, 
01:26:19.928 (928279000)|SYSTEM_METHOD_EXIT|[40]|Messaging.sendEmail(LIST<Messaging.Email>)
01:26:19.928 (928407000)|SYSTEM_METHOD_ENTRY|[42]|String.valueOf(Object)
01:26:19.928 (928464000)|SYSTEM_METHOD_EXIT|[42]|String.valueOf(Object)
01:26:19.928 (928484000)|SYSTEM_METHOD_ENTRY|[42]|System.debug(ANY)
01:26:19.928 (928494000)|USER_DEBUG|[42]|DEBUG|&&&& Messaging.SendEmailResult[getErrors=();isSuccess=true;]

Any idea please?
thanks in advance.
Best Answer chosen by Admin (Salesforce Developers) 
CLKCLK

You should either use toaddress or targetobjectId not both.

All Answers

CLKCLK

You should either use toaddress or targetobjectId not both.

This was selected as the best answer
Vinit_KumarVinit_Kumar

Agreed with Chetan,you should not be using both,also you should check no.of Email Invocation in the debug logs to see if there is any Email invocation or not.

Cloud CredenceCloud Credence
Hi,

Check ur SPAM folder.

Sometimes emails from Salesforce goes to SPAM.

Thanks
Praveen NPraveen N

Thanks to all... the code was fine. we can use both. there was some admin settings issue in sandbox. Exactly i too dont know whihc settings they have done. after that same code was working fine.