0

我正在研究从用户那里收集某些用户信息并通过电子邮件发送确认的网络表单。我正在尝试使用以下方法发送 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)
            {
            }

        }
4

1 回答 1

0

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

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

因此,要修复确保您添加到 ListDictionary 的所有值都转换为字符串,即

objVR.BookingDate.ToString()
于 2012-06-26T08:35:06.417 回答