none
Merging Non-email fields into Outlook Email body RRS feed

  • Question

  • Hello,

    I am trying to use Outlook Templates to create targeted emails.  I used Outlook to create a template that included a logo image and the following text:


     Dear $$CustomerName$$,

     Your recent inquiry has been assigned Case Number:  $$CaseReference$$ has been received by User Support.  We will respond as soon as possible.

      
     Thank you.

     Your Supprt Team.

    The message format is html.

    I use the following C# class to insert fields into the email message body:

            public void CreateItemFromTemplate(string emailTo, string emailCC, string emailSubject,
                string emailTemplate, string[] pFields, string[] Values)
            {
                Microsoft.Office.Interop.Outlook._Application outApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook._MailItem outMail = outApp.CreateItemFromTemplate(emailTemplate) as Microsoft.Office.Interop.Outlook.MailItem;
                outMail.To = emailTo;
                outMail.CC = emailCC;
                outMail.Subject = emailSubject;
                string mailBody = outMail.HTMLBody.ToString();

                for (int i = 0; i < pFields.Length; i++)
                {

                    string CurrentField = pFields[i];
                    string CurrentValue = Values[i];
                    string searchBody = "$$" + CurrentField + "$$";
                    
                    mailBody.Replace(searchBody, CurrentValue);
                }

                outMail.HTMLBody = mailBody;
                outMail.Send();
               
            }

    The program sends the email without replacing my tags with the field values.  Is there a better approach for accomplishing the task?

    This is not a bulk mailing project. My use case is to take data from a windows form, create an email from a template, insert data from the form into the body, and send the email.

    Thanks for your assistance.

    Matt Paisley
    Woodbridge, CT USA


    Matthew Paisley

    Sunday, August 12, 2012 2:27 AM

Answers

  • this is c# - all string functions that modify value return new strings, strings are immutable.

    you have to change your line

    mailBody.Replace(searchBody, CurrentValue);

    to

    mailBody = mailBody.Replace(searchBody, CurrentValue);

    • Marked as answer by Matt Paisley Sunday, August 12, 2012 2:46 PM
    Sunday, August 12, 2012 5:35 AM

All replies

  • this is c# - all string functions that modify value return new strings, strings are immutable.

    you have to change your line

    mailBody.Replace(searchBody, CurrentValue);

    to

    mailBody = mailBody.Replace(searchBody, CurrentValue);

    • Marked as answer by Matt Paisley Sunday, August 12, 2012 2:46 PM
    Sunday, August 12, 2012 5:35 AM
  • Thanks very much for your help DamianD.  This resolved the problem.

    Matthew Paisley

    Sunday, August 12, 2012 2:46 PM