+ Start a Discussion
shakila Gshakila G 

Single Send Email Functionality not working Properly?

Hi All, 
I have written an send email Functionality to send two different quote template based on the selected Template.

I passed the VF Constructor in Page Button. Sometimes its working fine and  some time it is not working Properly(Email is not triggering from salesforce just inserting the attachment in Salesforce )

Kindly assist me on this.It's very urgent kindly help me on this

MY Apex Class:
public class GenerateQuoteSpec{

    Public string QuoteId;
    public Quote Quot = new Quote();
    public Quote getQuot() { return Quot; }
    public Quote Quotedata= new Quote();
    public Blob content;
    string Contentname;
    string Contentname1;
    public Contact con { get; set; }
    public String subject { get; set; }
    public String emailbody { get; set; }
    public string CC{get;set;}
    List<String> CCAddress = new List<String>();
    
        
    public GenerateQuoteSpec(ApexPages.StandardController controller) {
        QuoteId = ApexPages.currentPage().getParameters().get('id');
        
        Quotedata= [select id,Name,Customer_name__c,QuoteNumber,Email,Letter_Head_New__c, Project_Type__c,ContactId from Quote where id =: QuoteID];         
           
           
      
        subject = 'Ankidyne Quotation for '+Quotedata.Project_Type__c +' '+'('+ Quotedata.QuoteNumber+')';
        emailbody = 'Greetings from Ankidyne!\n\nPlease find the quotation for our products. kindly approve our quotation to serve your organization.\n\nPlease Check the Quote Pdf attached for further information.\n\n';
    }

    public void generatepdf(){
        try {
            Contentname = 'Ankidyne Quotation for '+Quotedata.Project_Type__c +' '+'('+ Quotedata.QuoteNumber+')';
            Contentname1 = 'Ankidyne Quotation for '+Quotedata.Project_Type__c +' '+'('+ Quotedata.QuoteNumber+')';
            pageReference pdfpage;
            if(Quot.Quote_With_Tech_Spec__c == true) {
                system.debug('QuoteId'+QuoteId);
                pdfpage = page.GST_Image_Discount;
            }
             if(Quot.Quote_Without_Tech_Spec__c== true) { 
                system.debug('QuoteId'+QuoteId);
                pdfpage = page.GST_No_Discount;
            }
            pdfpage.getParameters().put('id',QuoteId);
            pdfpage.setRedirect(true);
            content = pdfpage.getcontent();
            Attachment att = new Attachment(ParentId=QuoteId, Body=content, ContentType='.pdf');
            // save this content in attachment
            if(Quot.Quote_With_Tech_Spec__c == true) {
                att.Name = Contentname;
            } if(Quot.Quote_Without_Tech_Spec__c== true) {
                att.Name = Contentname1;
            }
            insert att;
        }
        catch(exception ex) {
            apexpages.Message emsg = new apexpages.Message(apexpages.severity.WARNING,ex.getmessage());
            ApexPages.addMessage(emsg);
        }
    } 
    
    public pagereference save() {
        if((Quot.Quote_With_Tech_Spec__c == false && Quot.Quote_Without_Tech_Spec__c == false) || 
                (Quot.Quote_With_Tech_Spec__c == true && Quot.Quote_Without_Tech_Spec__c == true)) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Please select one option to continue'));
            return null;
        }
        generatepdf();
        pageReference returnpage = new pageReference('/'+QuoteId);
        return returnpage;
    }  
    
    public pagereference saveandsend() {
    
     con =  [select ID,Email,Email_Additional__c,CCEmail__c from Contact where ID=:Quotedata.ContactId] ; 
     
        if((Quot.Quote_With_Tech_Spec__c == false && Quot.Quote_Without_Tech_Spec__c == false) || 
                (Quot.Quote_With_Tech_Spec__c == true && Quot.Quote_Without_Tech_Spec__c == true)) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Please select one option to continue'));
            return null;
        }
        generatepdf(); 
        string[] Emailaddr = new string[]{};
        String[] ccAddresses = new String[]{};
        IF(CC!=Null || CC!='')
        {
        CCAddress=CC.split(',');
        }
        IF(con.Email!=Null || con.Email!='' || con.Email_Additional__c!=Null || con.Email_Additional__c!=''){
        Emailaddr.add(con.Email);
        Emailaddr.add(con.Email_Additional__c);
       }
        try {
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            attach.setFileName(Contentname+'.pdf');
            attach.setBody(content);
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(Emailaddr);
            email.setBccSender(true);
            email.setccAddresses(CCAddress);
            system.debug(emailbody);
            emailbody = emailbody.replace('\n','<br>');
            system.debug(emailbody);
            email.setSubject(Contentname);
            email.setHtmlBody(emailbody);
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {attach}); 
            system.debug('QuoteContactId' + con);
            
            // Send the email
             Messaging.SendEmailResult [] sendResult  = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
            
           } Catch(Exception ex){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'The Email Address Provided in the contact information is not valid one and the Email address is:'+ex.getmessage()));
        }
        
        pageReference returnpage = new pageReference('/'+QuoteId);
        return returnpage;
    }
    
    public pagereference cancel() {
        pageReference returnpage = new pageReference('/'+QuoteId);
        return returnpage;
    }         
}




 
AshishkAshishk
Did you check "Deliverability", is it set to All mails?