如何将参数从C#Windows应用程序传递到HTML电子邮件模板以发送电子邮件

时间:2013-04-05 12:37:47

标签: c# html email windows-applications

您好我需要在c#windows应用程序中使用模板发送电子邮件。我创建了一个模板,但我无法通过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模板中,我在Textboxes的同一页面中获取该参数的所有参数。请帮帮我

1 个答案:

答案 0 :(得分: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");

这是图片:

enter image description here

愿这个答案能帮到你。

干杯!