[英]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. 我正在使用Web表单,该表单从用户那里收集某些用户信息并通过电子邮件发送确认。 I am trying to user the following approach to send the HTML email as it make managing HTML based email easy.
我正在尝试使用以下方法发送HTML电子邮件,因为它使基于HTML的电子邮件的管理变得容易。
https://gist.github.com/1668751 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.
以下代码行生成错误
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(); 字符串消息= 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. 正如HatSoft指出的,ListDictionary接受对象作为键和值,因此看起来应该可以工作。
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. 但是在此处http://msdn.microsoft.com/zh-cn/library/0002kwb2.aspx上阅读CreateMailMessage()方法的文档表示,您需要将值转换为字符串,否则它将引发ArgumentException。
So to fix make sure all values you add to the ListDictionary are converted to string ie 因此,要解决问题,请确保将添加到ListDictionary的所有值都转换为字符串,即
objVR.BookingDate.ToString()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.