Use MailMergeLib as mail client library

Mar 2, 2011 at 10:05 AM
Edited Mar 2, 2011 at 3:26 PM

As discussed on the following web site there are some issues with the System.Net.Mail class (some bugs and RFC violations):

For future NSurvey versions it may be a good idea to switch to this library, or at least implement some of the suggested bug fixes and workarounds in the NSurvey application.

In addition the SendEmail function in the SystemWebEmailing class does not convert a HTML formatted body to plain text (bodyPlain) for the "alternatePlain" view.  As a result special HTML characters will show up in the plain text version of the e-mail.  Currently I'm using the StripHTML function from "The Code Project" to convert the HTML body to plain text: 
MailMergeLib uses the Microsoft open source Html Agility Pack (HtmlAgilityPack.dll) for converting the HTML body to plain text, so this may be a better alternative for StripHTML.

Also the SendEmail function should first create the "alternatePlain" view, and then the "alternateHtml" to make sure that the HTML version is displayed in the Thunderbird e-mail client (which seems to prefer the last view).


string bodyPlain = StripHTML(mail.Body);

AlternateView alternatePlain = AlternateView.CreateAlternateViewFromString(bodyPlain, null, "text/plain");
alternatePlain.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;

AlternateView alternateHtml = AlternateView.CreateAlternateViewFromString(bodyHtml, null, "text/html");
alternateHtml.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;

objMessage.BodyEncoding = System.Text.Encoding.UTF8;

smtp.Port = Int32.Parse(EmailConfig.SmtpServerPort);
smtp.EnableSsl = Boolean.Parse(EmailConfig.SmtpServerEnableSSL);
smtp.Send(objMessage );



The EnableSsl SMTP server parameter should also be supported in the config file and SendEmail function:

/// <summary>
/// Enable SSL for SMTP server
/// </summary>
public static string SmtpServerEnableSSL
		NameValueCollection config = (NameValueCollection)ConfigurationManager.GetSection("nSurveySettings");
		if (config == null)
			config = ConfigurationManager.AppSettings;
		return config["NSurveySMTPServerEnableSsl"];


Best Regards,




Mar 6, 2011 at 9:38 AM

Hello Peter,

Thank you for your contribution and good suggestions. We've already applied some of them to a new minor release we hope to publish a.s.a.p.
The curent mailing features are certainly not up to level and need some of the fixes you suggested.

Kind regards