2013-04-05 105 views
0

嗨我需要在c#windows应用程序中使用模板发送电子邮件。我创建了一个模板,但我无法通过html模板传递参数。这是我正在使用的模板。如何将参数从C#Windows应用程序传递到HTML电子邮件模板发送电子邮件

对于这个HTML模板,我打电话给我的Windows应用程序,并通过Gmail邮件发送。我能够发送邮件但无法将参数传递给它。请帮我out.Here就是我在我的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("[email protected]", "*******"); 
    //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("[email protected]"); 
    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模板这里我得到在文本框同一页的所有参数调用的代码。请帮我解决

+0

你的模板是什么样的?还是你问什么模板的最佳格式? – 2013-04-05 12:39:24

+0

当你成功时,message.Body会被设置为什么?文本是根据文件读出的吗? – Penfold 2013-04-05 13:38:37

+0

在重新阅读你的问题......它似乎你正在寻找最好的方式来将你的窗口中的控件(文本框等)的值注入到模板html body中的模板位置?我对么?如果是这样,你能提供模板文件吗?通常我会用一些正则表达式来替代它,但这取决于你的模板的格式。 – Penfold 2013-04-05 13:41:33

回答

0

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

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

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

愿这个答案可以帮到你。

干杯!