【问题标题】:How to pass parameters into an HTML email template from C# Windows application to Send an email如何将参数从 C# Windows 应用程序传递到 HTML 电子邮件模板以发送电子邮件
【发布时间】:2013-03-27 21:13:07
【问题描述】:

您好,我需要使用 c# windows application 中的模板发送电子邮件。我已经创建了一个模板,但我无法通过 html 模板传递参数。这是我正在使用的模板。

对于这个 HTML 模板,我在我的 Windows 应用程序中调用它并通过 gmail smtp 发送。我能够发送邮件,但无法将参数传递给它。请帮帮我。这是我在 Windows 应用程序中调用的代码

try
{
  using (StreamReader reader = File.OpenText("H:\\Visitor Management_Project\\Visitor Management_Project\\Visitor Management_Project\\EmailTemplate.htm"))
  {
     SmtpClient SmtpServer = new SmtpClient("173.194.67.108", 587);
     SmtpServer.UseDefaultCredentials = false;
     SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
     SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu@gmail.com", "*******");
     //SmtpServer.Port = 587;
     SmtpServer.Host = "smtp.gmail.com";
     SmtpServer.EnableSsl = true;
     message = new MailMessage();
     message.Subject = "Visitor Arrived";
     //message.SubjectEncoding = System.Text.Encoding.UTF8;
     message.IsBodyHtml = true;
     message.Body = "EmailTemplate.htm";
     //message.BodyEncoding = System.Text.Encoding.UTF8;
     message.From = new MailAddress("ambarishkesavarapu@gmail.com");
     message.To.Add(lblCPEmail.Text);
     message.Priority = MailPriority.High;
     message.Body = reader.ReadToEnd();
     message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
     SmtpServer.Send(message);
   }
 }
 catch (Exception ex)
 {
   MessageBox.Show(ex.Message);
 }

如何将参数添加到 HTML 模板中,我在文本框的同一页面中获取该模板的所有参数。请帮帮我

【问题讨论】:

  • 您的模板是什么样的?或者您是否也在问最好的模板格式是什么?
  • 当你bedug时,message.Body会被设置成什么?文本是否会从文件中读出?
  • 在重新阅读您的问题时......您似乎正在寻找将您的值从 Windows 窗体中的控件(文本框等)注入模板 html 中的模板位置的最佳方法身体?我对么?如果是这样,你能提供模板文件吗?通常我会用一些正则表达式替换来做到这一点,但这取决于你模板的格式。

标签: c# html email windows-applications


【解决方案1】:

我想你自己已经找到了答案,但我会继续发布答案。

如果您使用的是 Windows 窗体而不是 Windows 演示窗体(不同之处仅在于设计部分和许多 Windows 窗体上没有的新功能),我所做的是这样的(发送电子邮件):

public void SendEmail(string _from, string _fromDisplayName, string _to, string _toDisplayName, string _subject, string _body, string _password)
    {
        try
        {
            SmtpClient _smtp = new SmtpClient();

            MailMessage _message = new MailMessage();

            _message.From = new MailAddress(_from, _fromDisplayName); // Your email address and your full name

            _message.To.Add(new MailAddress(_to, _toDisplayName)); // The recipient email address and the recipient full name // Cannot be edited

            _message.Subject = _subject; // The subject of the email
            _message.Body = _body; // The body of the email

            _smtp.Port = 587; // Google mail port
            _smtp.Host = "smtp.gmail.com"; // Google mail address
            _smtp.EnableSsl = true;
            _smtp.UseDefaultCredentials = false;
            _smtp.Credentials = new NetworkCredential(_from, _password); // Login the gmail using your email address and password

            _smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtp.Send(_message);

            ShowMessageBox("Your message has been successfully sent.", "Success", 2);
        }

        catch (Exception ex)
        {
            ShowMessageBox("Message : " + ex.Message + "\n\nEither your e-mail or password incorrect. (Are you using Gmail account?)", "Error", 1);
        }
    }

我是这样使用它的:

SendEmail(textBox2.Text, textBox5.Text, textBox3.Text, "YOUR_FULL_NAME", textBox4.Text, textBox6.Text, "YOUR_EMAIL_PASSWORD");

图片如下:

希望这个答案对你有所帮助。

干杯!

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 2020-01-20
    • 2015-06-28
    相关资源
    最近更新 更多