簡體   English   中英

如何從C#Windows應用程序將參數傳遞到HTML電子郵件模板中以發送電子郵件

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

嗨,我需要在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模板中,我在文本框中的同一頁面中獲取了該參數的所有參數。 請幫幫我

我認為您已經自己找到了答案,但是我會繼續發布答案。

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

我這樣使用它:

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

這是圖片:

在此處輸入圖片說明

希望這個答案對您有幫助。

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM