1

嗨,我需要使用 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 模板中,我在文本框的同一页面中获取该模板的所有参数。请帮帮我

4

1 回答 1

0

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

如果您使用的是 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");

这是图像:

在此处输入图像描述

愿这个答案对你有所帮助。

干杯!

于 2014-12-30T10:30:28.743 回答