web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Sending a HTML email from within C# code

(0) ShareShare
ReportReport
Posted on by
 string emailHTML = "<html><head><style>table {border: 1px solid black; font-family: Arial, Helvetica, sans-serif;}"
+ "td, th {padding: 0.1cm 0.5cm; }tr:nth-child(even) {background-color: #dddddd;}"
+ "</style></head><body><table><tr><th colspan='2'>CONTACTS</th><th colspan='2'>OPPORTUNITIES</th></tr>"
 + "<tr><td>Newly created records total</td><td>" + totalNewRecords + "</td><td>Newly created records total</td><td>" + sumTotalOfImportedOppRecords + "</td></tr>"
 + "<tr><td colspan='2' align='center'><b>Totals for Contact records not imported</b></td>"
   + "<td  colspan='2' align='center'><b>Totals for Opportunity records not imported</b></td></td></tr>"
 + "<tr><td>Do not contact</td><td>" + totalNotImported_DoNotContact + "</td><td>Existing Opportunities</td><td>" + totalNonImportedOpps + "</td></tr>"
 + "<tr><td>Ref Match</td><td>" + totalRefMatch + "</td><td>Error records</td><td>" + totalNonImportedErrorOpps + "</td></tr>"
+ " <tr><td>Other Match</td><td>" + totalRefNonMatch + "</td><td colspan='2' align='center'><b>Totals for Opportunity records imported</b></td></tr>"
 + "<tr><td>Contacts with no Opportunities</td><td>" + totalContactsNotImported_NoOpps + "</td><td>Total Opportunities imported</td><td>" + totalNewlyImportedOpps + "</td></tr>"
 + "<tr><td>Error records</td><td>" + totalNotImportedErrorRecords + "</td><td>Total Non Opportunities imported</td><td>" + totalNewlyImportedNonOpps + "</td></tr> "
  + "<tr><td>Total Contacts in file</td><td>" + sumTotalOfContactRecords
  + "</td><td>Total Opportunities in file</td><td>" + sumTotalOfOppRecords + "</td></tr>"
 + "<tr><td>Total Contacts imported</td><td>" + totalNewRecords + "</td><td>Total Opportunities imported</td><td>" + sumTotalOfImportedOppRecords + "</td></tr>"
  + "<tr><td>Total Contacts not imported</td><td>" + TotalContactsNotImported + "</td><td>Total Opportunities not imported</td><td>" + TotalOpportunitiesNotImported + "</td></tr>"
+ "</table></body></html>";

                body.AppendLine(emailHTML);


I'm trying to send a CRM HTML email from within a C# console app. I need to have some variables in the HTML. It's working fine, but I'm hard coding the HTML in the code. I think there's a different/better way to do this?

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Thanks.

    Would you recommend putting the html in a configuration file?

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    To convert HTML to plain text refer 

    https://crmbusiness.wordpress.com/2014/09/29/crm-2013-convert-email-html-to-plain-text/

    Try the code below.

    Regex.Replace(emailHTML, @"<(.|\n)*?>", string.Empty);

    See the link below to send an email using c# code.

    https://dynamicscrmguru.com/2013/03/12/how-to-send-an-email-from-crm-2011-using-c/

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans