2011-12-20 4 views
0

예전에 전자 메일 양식 asp.net에서 양식 필드를 보내려면이 형식을 사용했지만 HTML 전자 메일을 더 형식화하려고합니다. 누군가에게 나를 도울 수있는 메시지에 텍스트 상자 값을 전달하는 방법을 알아?전자 메일 본문 ASP.NET에 양식 텍스트를 추가하는 방법

//OLD WAY 
message.Body += "<b>Preferred contact method: </b> " + drp_contact_method.SelectedValue + "<br/>";// 


protected void btnAction_Click(object sender, EventArgs e) 
    { 
     if ((Page.IsValid)) 
     { 
      if (dpAction.SelectedValue.ToString()=="Submit") 
      { 
      // define SMTP client 

      SmtpClient smtp = new SmtpClient("IP HERE"); 
      //smtp.EnableSsl = true; 
      // smtp.UseDefaultCredentials = true; 
      //create the mail message 
      MailMessage message = new MailMessage(); 

      //From address will be given as a MailAddress Object 
      message.From = new MailAddress("[email protected]"); 


      //To address collection of MailAddress 
      message.To.Add("[email protected]"); 
      message.Subject = "Pre-Registration Form"; 


      // Change to false to not include HTML 

      string bodyHTML = string.Empty; 
      string bodyPlain = string.Empty; 
      message.IsBodyHtml = true; 

      bodyHTML = @"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 
'http://www.w3.org/TR/html4/loose.dtd'> 
<html lang='en'> 
<head> 
</head> 
<body> 
<table style='width:800px; border:1px solid #000;' cellpadding='10' cellspacing='0' align='center'> 
<tr> 
<td colspan='2' style='background-color:#009797; border-bottom:1px solid #000;'> 
<h1 style='text-align:center; color:#FFF;'>Pre-Registration Information</h1></td> 
</tr> 
    <td style='width:600px; vertical-align:top;'> 
<h4 style='font-family:Arial, Helvetica, sans-serif; color:#000;'><u>PATIENT INFORMATION</u></h4> 
<p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Last Name:</p> 
+ txtLastName.Text + 
+0

일반적으로 이와 같은 클래스 파일에 HTML을 작성하지 않고 사용자 정의 컨트롤을 만들고 사용자 정의 컨트롤 내에서 비즈니스 로직을 뒤에 배치합니다. 그런 다음 전자 메일 보내기 클래스/메서드에서 컨트롤을 페이지에 던져서 실행되는 것과 같은 방법으로 사용자 정의 컨트롤을 실행할 수 있습니다. 단, 클라이언트에 보내는 HTML 문자열을 다시 얻을 수 있다는 점만 다릅니다. –

답변

0

가장 깨끗한 방법은 string.Format입니다. 먼저 bodyHTML에있는 것처럼 HTML을 정의하지만 자리 표시자를 {0}{1}과 같이 문자열에 넣으십시오. 그런 다음, 다음과 같은 양식 변수를 연결 :

var body = string.Format(
    bodyHTML, 
    HttpUtility.HtmlEncode(yourTextBox.Text), 
    HttpUtility.HtmlEncode(yourOtherTextBox.Text)); 

를 HTML 제대로 인코딩 된 두 값으로, yourOtherTextBox.TextyourTextBox.Text{1}으로 {0}을 대체 할 것이다.

+0

몇 가지 변수가 있습니다. 약 20 가지가 있습니다. 정확히 코드에서 변경해야하는 것을 보여 주시겠습니까 – user1091438

+0

형식 문자열에 원하는만큼 많은 자리 표시자가 포함될 수 있으므로이 기술이 제대로 작동해야합니다. – Jacob