繁体   English   中英

基于HTML的电子邮件无法在asp.net网络表单中正常工作

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

我正在使用Web表单,该表单从用户那里收集某些用户信息并通过电子邮件发送确认。 我正在尝试使用以下方法发送HTML电子邮件,因为它使基于HTML的电子邮件的管理变得容易。

https://gist.github.com/1668751

我对代码进行了必要的更改,但无法正常工作。 我阅读了其他相关文章以使其工作,但我做错了事。

以下代码行生成错误The replacements dictionary must contain only strings.

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

我对代码进行了很多更改,但它似乎对我不起作用。 我希望能帮助使此代码正常工作。

如果我用其他一些更改注释了上面的代码行,那么我可以发送电子邮件,但不能替换令牌。 也欢迎任何简单的替换令牌的方法。

以下是我现在正在使用的完整代码

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());

字符串消息= 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)
            {
            }

        }

正如HatSoft指出的,ListDictionary接受对象作为键和值,因此看起来应该可以工作。

但是在此处http://msdn.microsoft.com/zh-cn/library/0002kwb2.aspx上阅读CreateMailMessage()方法的文档表示,您需要将值转换为字符串,否则它将引发ArgumentException。

因此,要解决问题,请确保将添加到ListDictionary的所有值都转换为字符串,即

objVR.BookingDate.ToString()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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