简体   繁体   English

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

[英]How to pass parameters into an HTML email template from C# Windows application to Send an email

Hi I need to send an email using template in c# windows application . 嗨,我需要在c#Windows应用程序中使用模板发送电子邮件。 I had created a template but i am unable to pass the parameters through the html template. 我已经创建了模板,但是无法通过html模板传递参数。 Here is the template which i am using. 这是我正在使用的模板。

For this HTML Template i am calling this in my windows application and sending through gmail smtp. 对于此HTML模板,我在Windows应用程序中调用此模板并通过gmail smtp发送。 I am able to send the mail but unable to pass the parameters into it. 我能够发送邮件,但无法将参数传递给它。 Please Help me out.Here is the code where i am calling in my windows application 请帮帮我。这是我在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);
 }

How to add Parameters into HTML Template where i am getting all the parameters of that in the same page in Textboxes. 如何将参数添加到HTML模板中,我在文本框中的同一页面中获取了该参数的所有参数。 Please help me out 请帮帮我

I think you already discovered the answer yourself, but I will keep posting the answer instead. 我认为您已经自己找到了答案,但是我会继续发布答案。

In case you are using Windows Forms and not Windows Presentation Forms (The different only the design part and many new features that does not have on Windows Forms), what I have done is like this (to send email): 如果您使用的是Windows窗体而不是Windows Presentation窗体(仅设计部分和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);
        }
    }

And I am using it like this: 我这样使用它:

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

Here is the image: 这是图片:

在此处输入图片说明

May this answer would help you. 希望这个答案对您有帮助。

Cheers! 干杯!

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

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