+ Start a Discussion
PrabhataPrabhata 

How to change email Id displayed in from address in a customized email message

Hi Experts,
Public void SendOrderEmail(){

        String Uname, ToAddress;
        String AccFirstName, AccLastName, ContFirstName, ContLastName; /* Added for UpperCase */        
        if(ConId==null && AId!=null){
            List<Account> acc = [SELECT First_Name__c, Last_Name__c, Email_Address__c  FROM Account WHERE Id = :AId limit 1];
            /* Added for UpperCase *******************************************Start*/
            AccFirstName = acc[0].First_Name__c;
            AccFirstName = AccFirstName.substring(0,1).toUpperCase() + AccFirstName.substring(1).toLowerCase();
            AccLastName = acc[0].Last_Name__c;
            AccLastName = AccLastName.substring(0,1).toUpperCase() + AccLastName.substring(1).toLowerCase();
            Uname = AccFirstName+' '+AccLastName;
            /* Added for UpperCase ********************************************End*/
           // Uname = acc[0].First_Name__c+' '+acc[0].Last_Name__c;
            ToAddress = acc[0].Email_Address__c; 
        }else{
          //  List<Contact> con = [SELECT Name, Email_Address__c  FROM Contact WHERE Id = :ConId limit 1];
          //  Uname = con[0].Name;
            /* Added for UpperCase *******************************************Start*/
            List<Contact> con = [SELECT FirstName, LastName, Email_Address__c  FROM Contact WHERE Id = :ConId limit 1];
            ContFirstName = con[0].FirstName;
            ContFirstName = ContFirstName.substring(0,1).toUpperCase() + ContFirstName.substring(1).toLowerCase();
            ContLastName = con[0].LastName;
            ContLastName = ContLastName.substring(0,1).toUpperCase() + ContLastName.substring(1).toLowerCase();
            Uname = ContFirstName+' '+ContLastName;
            /* Added for UpperCase ********************************************End*/
            ToAddress = con[0].Email_Address__c; 
        }
        
        String OrderAmt = String.valueOf(odr.TotalAmount);
        
        String EmailContent = '<table style="background: #E32237;color:#fff;">';
        EmailContent += '<tr><td colspan=2 style="padding-bottom:10px;font-weight:bold;">Cher '+Uname+',</td></tr>';
        EmailContent += '<tr><td colspan=2 style="padding-bottom:5px;">'+Label.Order_Summary_Page_Success_Message+'</td></tr>';
        EmailContent += '<tr><td style="font-weight:bold;">'+Label.Order_Summary_Order_Number+'</td><td>'+odr.OrderNumber+'</td></tr>';
      //  EmailContent += '<tr><td style="font-weight:bold;">'+Label.Order_Summary_Seasonal_Offers+'</td><td>'+odr.Seasonal_Offers__c+'</td></tr>';
      //  EmailContent += '<tr><td style="font-weight:bold;">'+Label.Order_Summary_Permanent_Offers+'</td><td>'+odr.Permanent_Offers__c+'</td></tr>';
        EmailContent += '<tr><td style="font-weight:bold;">'+Label.Order_Summary_Order_Amount+'</td><td>'+OrderAmt.replace('.',',')+'€</td></tr>';
        EmailContent += '<tr><td style="font-weight:bold;width: 35%;">'+Label.Order_Summary_Status+'</td><td style="width: 65%;">Commande reçue</td></tr>';
        EmailContent += '<tr><td colspan=2 style="padding:10px 0 3px;font-weight:bold;">'+Label.Order_Summary_Page_Order_Products+'</td></tr>';
        EmailContent += '<tr><td colspan=2 style="padding-bottom:10px;"><table style="color:#fff;border-collapse: collapse;border-spacing: 2px;" border=0>';
        EmailContent += '<tr style="font-weight:bold;background:#B50E20;"><td style="padding:5px">'+Label.Order_Summary_Page_Product+'</td><td style="padding:5px">'+Label.Order_Summary_Page_Product_Code+'</td><td style="padding:5px">'+Label.Order_Summary_Page_Quantity+'</td><td style="padding:5px">'+Label.Order_Summary_Page_Unit_Price+' (€)</td><td style="padding:5px">'+Label.Order_Summary_Page_Total_Price+' (€)</td></tr>';                
        for(OrderItemWrapperClass oi : OrderItemWrapperList){
            String Qty = String.valueOf(oi.Qty);
            String Prce = String.valueOf(oi.Prce);
            String Ttl = String.valueOf(oi.Ttl);
            EmailContent += '<tr style="color:#000;font-size:13px;background:#fff"><td style="padding:5px">'+oi.Prod+'</td><td style="padding:5px">'+oi.PCode+'</td><td style="padding:5px;text-align:right;">'+Qty.replace('.',',')+'</td><td style="padding:5px;text-align:right;">'+Prce.replace('.',',')+'</td><td style="padding:5px;text-align:right;">'+Ttl.replace('.',',')+'</td></tr>';                
       
        }
        String freeSeasonalProduct = String.valueOf(odr.Seasonal_Free_Product__c);
        String freePermanentProduct = String.valueOf(odr.Permanent_Free_Product__c);
        String freeSeasonalQuantity = String.valueOf(odr.Seasonal_Offers__c);
        String freePermanentQuantity = String.valueOf(odr.Permanent_Offers__c);
        
        if((freeSeasonalQuantity != null && Integer.valueOf(freeSeasonalQuantity) != 0) || (freePermanentQuantity != null && Integer.valueOf(freePermanentQuantity) !=0)){
            EmailContent += '</table><tr><td colspan=2 style="padding:10px 0 3px;font-weight:bold;">'+Label.Order_Summary_Page_Free_Products+'</td></tr>';
            EmailContent += '<tr><td colspan=2 style="padding-bottom:10px;"><table style="color:#fff;border-collapse: collapse;border-spacing: 2px;" border=0>';
            EmailContent += '<tr style="font-weight:bold;background:#B50E20;"><td style="padding:5px">'+Label.Order_Summary_Page_Product+'</td><td style="padding:5px">'+Label.Order_Summary_Page_Product_Category+'</td><td style="padding:5px">'+Label.Order_Summary_Page_Quantity+'</td></tr>';                                 
            
            if(freeSeasonalQuantity != null && Integer.valueOf(freeSeasonalQuantity) != 0){
            EmailContent += '<tr style="color:#000;font-size:13px;background:#fff"><td style="padding:5px">'+freeSeasonalProduct.replace('.',',')+'</td><td style="padding:5px">'+Label.Order_Summary_Seasonal_Offers+'</td><td style="padding:5px;text-align:left;">'+freeSeasonalQuantity.replace('.',',')+'</td></tr>';                
            }
            if(freePermanentQuantity != null && Integer.valueOf(freePermanentQuantity) !=0){
            EmailContent += '<tr style="color:#000;font-size:13px;background:#fff"><td style="padding:5px">'+freePermanentProduct.replace('.',',')+'</td><td style="padding:5px">'+Label.Order_Summary_Permanent_Offers+'</td><td style="padding:5px;text-align:left;">'+freePermanentQuantity.replace('.',',')+'</td></tr>';                
            }
        }      
        EmailContent += '</table><tr><td colspan=2 style="padding-bottom:10px;"><a style="color:#fff;" href="'+Label.Order_Page_Domain_Name+'/OrderCreation?id='+AId+'&cid='+ConId+'">'+Label.Order_Summary_Page_Click_Here+'</a> '+Label.Order_Summary_Page_Clickhere_Message+'</td></tr>';
        EmailContent += '<tr><td colspan=2 style="font-weight:bold;">'+Label.Order_Summary_Page_Thanks+',<br/>'+Label.Order_Summary_Page_Mars_Chocolate+' </td></tr>';
        
        EmailContent += '</table>';    
        
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] { ToAddress });
        email.setSubject(Label.Order_Summary_Subject); 
        email.setHTMLBody(EmailContent);
        
        Messaging.sendEmail(new Messaging.Email[] { email });
                
    }
In the output, I am getting an email in which I am unable to change the email displayed in From field.

For example:
From: Order Summary Site Guest User to you <xyz@gmail.com>
Date: Wed, Jan 28,2015 at 5.24 PM
Subject: Sandbox: Order Summary
Using setSenderDisplayName, setReplyTo, I am able to change "Order Summary Site Guest User to you" part, not "xyz@gmail.com". Is there any way to change the email address appearing by default.
 
Vishnu VaishnavVishnu Vaishnav
Hi,

Here is some steps to change from  address :

Go to :
Email Administration ---
    Organization-Wide Addresses

Add new Organization-Wide Email Addresses.
After adding your email u will get a mail on this mailid, verify your mail address.

:::======================================================================:::
Qusetion Solved ? then mark as best answer to make helpful to others .....

 
PrabhataPrabhata
Hi Vishnu,

I have tried this option as well, but couldn't get the desired solution. Is there any other way you are aware of for this ?
Vishnu VaishnavVishnu Vaishnav
Hi ,

Confirm that your email is verified or setting looks like below image :

User-added image