0

I'm trying to send an email with a HTML body, before i send the mail i want to modify some of the HTML. Basically like you would in jquery, i need to change a few text values inside of divs with certain IDs.

I tried to use https://github.com/jamietre/CsQuery but i dont find any info on ID selectors.

Can someone recommend any other solution?

This is my sendEmail code right now:

public static void sendEmail(String reciverMail, String Subject1, String Body1)
{
    if (reciverMail.Contains('@') && reciverMail != "")
    {
        using (StreamReader reader = File.OpenText(HttpContext.Current.Server.MapPath("~/mail.htm")))
        {

            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = false;
            client.Credentials = SmtpServerCredentials;
            client.Host = SmtpServerAddress;
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(SenderAddress, SenderName1);
            mail.To.Add(reciverMail);
            mail.Subject = Subject1;
            mail.IsBodyHtml = true;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            //mail.Body = Body1;
            mail.Body = reader.ReadToEnd();

            try
            {
                client.Send(mail);
            }
            catch (SmtpException ex)
            {
                string aaa = ex.Message;
            }
        }
    }
}
4
  • Can you show some pseudo code of what you expect to do, the input and expected output? Commented May 26, 2014 at 12:43
  • If you use something Postal to create your emails, you can have the full power of MVC/Razor for inserting content in the HTML page. You will find you don't need the jQuery approach then. Commented May 26, 2014 at 12:45
  • try using some library to handle HTML content like htmlagilitypack, you can even use XPath, or I guess Linq-To-XML can also work. Commented May 26, 2014 at 12:51
  • Is this for a website, or an application? Commented May 26, 2014 at 12:56

2 Answers 2

6

You can use the Html Agility Pack: It's a lovely HTML parser that is commonly recommended for this kind of work.

You can then write the following:

HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml("Your content");
string centralDiv = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='centralDiv ']").InnerHtml;
Sign up to request clarification or add additional context in comments.

Comments

0

If this is for a C# website (and not a desktop application), you are better off using something like Postal (http://aboutcode.net/postal/) to generate your HTML template. You then can forget the complexities of DOM manipulation and use the tools you know (views with Razor syntax etc).

If you are working with an application, Postal also supports working outside of a website (http://aboutcode.net/postal/outside-aspnet.html), but the technology is the same as generating an MVC razor view so you need to know a little about MVC/Razor & Views.

You can install Postal free via NuGet. The credentials will go in your web.config (sendgrid is a really nice robust, free, option for sending less than 10,000s of emails a month).

Your action code becomes something simple like:

public static void sendEmail(String reciverMail, String Subject1, String Body1)
{
    dynamic email = new Email("MyTemplate");
    email.To = reciverMail;
    email.Body1 = Body1;            // Just make up your own properties to send in values!
    email.Subject = Subject1;
    email.Send();
}

You simply create a Razor view as your template:

e.g. ~\Views\Email\MyTemplate.cshtml

To: @ViewBag.To
From: [email protected]
Subject: @ViewBag.Subject

Hello,
<div>
    @ViewBag.Body1
</div>

You can see it can be expanded to insert as many elements as you want, anywhere in the email, in a really readable/maintainable format.

Html & Plain text emails:

Postal also supports sending your emails as both text and HTML versions by providing two templates per email. http://aboutcode.net/postal/multi-part.html

I have used Postal on several projects and find for the ease of maintenance alone it is worth it. You should not have to fiddle about with DOM manipulation for something as simple as email templates. You may have asked for "DOM manipulation", but this is a far more practical way forward.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.