+ Start a Discussion
Ashish_BorateAshish_Borate 

SingleEmailMessage not working properly.

Hi,

 

I just want to send a single mail with an attachment, I have written something like this

Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
        String toAddresses = 'ashishborate@xyz.com';
        List<String> addr = new List<String>();
        addr.add(toAddresses);
        String subject ='Account CSV';
        email.setToAddresses(addr);
        email.setSubject(subject);
        email.setPlainTextBody('Account CSV');
        email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
        email.emailPriority='low';
        System.debug('Sending Email @@ '+email);
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        System.debug('SendEmailResult @@ '+r);

 Where, debug gives me, but I am not getting any mail. Kindly help.

SendEmailResult @@(Messaging.SendEmailResult[getErrors=();isSuccess=true;]

 Thanks,

Ashish.

SoCal AdminSoCal Admin

 

Where is your code placed?

 

There are limitations on whether or not Emails are sent out.  I think I had my code in the constructor and had to move it somewhere else.  It worked perfectly once I did.

crop1645crop1645

if running in a testmethod, emails are never sent out; you need to test via the UI

Ashish_BorateAshish_Borate

Hi,

 

I have written this code in one method named 'send()' in apex class, and calling that method through constructor.

Are there any ORG related settings to be done? (like, bounce, relay etc.)

 

Stuck on this, please help me out.

 

Thanks,

Ashish.

Mayank_JoshiMayank_Joshi

is this your valid email Address : ashishborate@xyz.com 

 

Please confirm ,and also check emails in Junk /Spam Folders as well . 

 

Warm Regard,

Ashish_BorateAshish_Borate

Hi Mayank,

 

This is not my original id.

The id I have stated in my code, is not showing any incoming mails sent through this code.

I have checked all possible places where it can get delivered, also tried the same, with 3 different email-ids.

 

:(

But, of no use.

Thanks,

Ashish.

Varun99Varun99

Hi Ashish,

 

Is it help full to you. i dont know but it is working file for multiple emails to send attachments

try it !

 

Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[]{};

Messaging.EmailFileAttachment pdfFileAttachment = new Messaging.EmailFileAttachment();
pdfFileAttachment.setBody(attach.body);
pdfFileAttachment.setFileName('file xlsx');
fileAttachments.add(pdfFileAttachment);
           
                       
mail.setToAddresses(toAddresses);
mail.setSubject(subject);     
mail.setplaintextBody(plainbody);
//mail.setFileAttachments(new Messaging.EmailFileAttachment{attach});
mail.setFileAttachments(fileAttachments );

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


/* Here toaddress is a list type */

public string To{get;set;}  // email recipient to address field

List<String> toAddresses = new List<String>();

toAddresses = To.split(',');

 

crop1645crop1645

hmm

 

 

the field email.priority is not even documented in the Apex developer documentation.  When I tried in execute anonymous, I get an NPE