+ Start a Discussion
Edwin VijayEdwin Vijay 

Send email from Site

Hi All,

 

I tried to send an email using the Messaging.singleemail..... but i get the Unauthorized page whenever this line of code executes...

 

I do not find any permissions in the SiteProfile to enable this..

 

Is it like i cannot call this method from a site... Pls suggest...

 

Thanks,

Edwin

Best Answer chosen by Admin (Salesforce Developers) 
BrianWKBrianWK

It looks like you're missing either a Contact reference or a populated TOAddress...

 

Right now that E-mail looks like this: Here's my object, Here's my body (template) and here is the reply to...

 

but no receipient!

 

One thing I like to do is the code below. This still sends the email, but if it fails it puts the failure in the debug log and adds an apex message.

 

list<Messaging.SingleEmailMessage> EmailList = new list<Messaging.SingleEmailMessage>(); EmailList.add(mail); List<Messaging.SendEmailResult> sendEmailResult = Messaging.sendEmail(emailList, false); system.debug('EmailErrors: '+ SendEmailResult); for(integer i=0; i<SendEmailresult.size(); i++) { if(SendEmailResult[i].isSuccess()==false ) { apexpages.message ErrorMsg = new ApexPages.Message(ApexPages.Severity.error, 'An error has occured sending your email please contact your Pharmacy OneSource Salesforce Administrator'); //see page 124 apexPages.addMessage(ErrorMsg); break; } }

 

All Answers

BrianWKBrianWK

Edwin1,

 

Where is your method stored? I had a similar issue -- but my SendEmail method is in a separate class outside of my visualforce controllers. In my case I had to go set permissions in that class so it was assessible by the site profile. Otherwise my controller called the method and returned a permission error.

Edwin VijayEdwin Vijay

Hi Brian,

 

My method is inside my contoller... So should i put my method in a seperate class and grant access to the site profile..

 

 

But, dont you think its weird... my controller also can be accessed by the site profile.. i could'nt understand then why this authorization comes up...

 

 

Thanks for your advice...

BrianWKBrianWK

I'm not sure why that would be happening. Could you post your code?

 

The only thing I can think of is that the method might be returning something that user doesn't have access too -- like a PageReference or something else.

 

Could you post your email method? Please make sure you use the little code button at the top of the screen and we can avoid all those annoying emoticons that op up wwith the <apex:page> tags!

Edwin VijayEdwin Vijay

Hi..

 

I have given my cutom object's ID as the targetobjectId.. would that be a problem..

 

 

/*Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId(sitereg.Id); mail.setTemplateId('00XR0000000DqVF'); mail.setReplyto('activateaccount@2qoxianxkg4fyxogkbtpmyuvz.in.sandbox.salesforce.com');*/ /* Send the mail */ /*Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });*/

 I just commented the above code and my site works fine.. except that i dont get an email :-)

 

Thanks for your time

 

JohannesBorrmanJohannesBorrman
where do you address the recipient? doesn't seem to be all code?!
BrianWKBrianWK

It looks like you're missing either a Contact reference or a populated TOAddress...

 

Right now that E-mail looks like this: Here's my object, Here's my body (template) and here is the reply to...

 

but no receipient!

 

One thing I like to do is the code below. This still sends the email, but if it fails it puts the failure in the debug log and adds an apex message.

 

list<Messaging.SingleEmailMessage> EmailList = new list<Messaging.SingleEmailMessage>(); EmailList.add(mail); List<Messaging.SendEmailResult> sendEmailResult = Messaging.sendEmail(emailList, false); system.debug('EmailErrors: '+ SendEmailResult); for(integer i=0; i<SendEmailresult.size(); i++) { if(SendEmailResult[i].isSuccess()==false ) { apexpages.message ErrorMsg = new ApexPages.Message(ApexPages.Severity.error, 'An error has occured sending your email please contact your Pharmacy OneSource Salesforce Administrator'); //see page 124 apexPages.addMessage(ErrorMsg); break; } }

 

This was selected as the best answer
Edwin VijayEdwin Vijay

Thanks a lot..

 

So if i am replacing my Targetobjectid with a valid contact,Lead or user id.. things should work fine..