简体   繁体   中英

HTML based email not working properly in asp.net web form

I am working on web form which collects certain user information from users and sends confirmation by email. I am trying to user the following approach to send the HTML email as it make managing HTML based email easy.

https://gist.github.com/1668751

I made necessary changes to the code but it is not working. I read other related article to make it work but i am doing something wrong.

Following line of code generates error The replacements dictionary must contain only strings.

MailMessage msgHtml = mailDef.CreateMailMessage(to, replacements, MessageBody, new System.Web.UI.Control());

I have made many change to the code but it doesnt seem to work for me. I would appreciate help to make this code work.

If i comment the above line of code with some other changes then i can send email but i can't replace the token. Any easy approach to replace token is also welcome.

Below is the Complete code i am using right now

String to, subject, Name;
subject = "Booking Confirmation";
Name = txtName.text;


ListDictionary replacements = new ListDictionary();
replacements.Add("<%Name%>", Name);
replacements.Add("<%Email%>", objVR.Email);
replacements.Add("<%CompanyName%>", objVR.CompanyName);
replacements.Add("<%BookingDate%>", objVR.BookingDate);
replacements.Add("<%BookingTime%>", objVR.TimeSlot);
replacements.Add("<%NoOfVisitors%>", objVR.NoOfVisitors);
replacements.Add("<%BookingCode%>", objVR.BookingUniqueID);


MailDefinition mailDef = new MailDefinition();

string MessageBody = String.Empty;
string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;

using (StreamReader sr = new StreamReader(filePath + @"\en\VREmailEnglish.htm"))
{
    MessageBody = sr.ReadToEnd();
}

MailMessage msgHtml = mailDef.CreateMailMessage(to, replacements, MessageBody, new System.Web.UI.Control());

string message = msgHtml.Body.ToString();

Helper.SendTokenEmail(to, subject, msgHtml, isHtml);




public static void SendTokenEmail(string to, string subject, string mailMessage, bool isHtml)
        {
            try
            {
                // Create a new message
                var mail = new MailMessage();

                // Set the to and from addresses.
                mail.From = new MailAddress("noreply@somedomain.net");
                mail.To.Add(new MailAddress(to));

                // Define the message
                mail.Subject = subject;
                mail.IsBodyHtml = isHtml;
                mail.Body = mailMessage.ToString();
                //Object userState = mailMessage;

                // Create a new Smpt Client using Google's servers
                var mailclient = new SmtpClient();
                mailclient.Host = "mail.XYZ.net";
                //mailclient.Port = 587; //ForGmail
                mailclient.Port = 2525;

                mailclient.EnableSsl = false;
                mailclient.UseDefaultCredentials = true;

                // Specify your authentication details

                mailclient.Credentials = new System.Net.NetworkCredential("noreply@somedomain.net", "XYZPassword");

                mailclient.Send(mail);
                mailclient.Dispose();
            }
            catch (Exception ex)
            {
            }

        }

As pointed out by HatSoft the ListDictionary accepts objects as key and value so this looks like it should work.

But reading the docs for the CreateMailMessage() method here http://msdn.microsoft.com/en-us/library/0002kwb2.aspx indicates you need to convert the value to a string otherwise it will throw an ArgumentException.

So to fix make sure all values you add to the ListDictionary are converted to string ie

objVR.BookingDate.ToString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM