2012-04-26 50 views
1

我有一個aspx窗體,並且有一堆文本框,下拉列表框,複選框。當這個輸入控件充滿了必要的數據時,我想以電子郵件的形式發送這些信息。假設我有一個帶有申請人姓名的文本框,列出了職業的下拉列表等。所以我會希望電子郵件中包含相同的信息,因爲ASP.NET - 以純文本電子郵件發送輸入數據

Name:Anthony Brian 
Occupation: Surgeon 
...... 

我該怎麼做?

編輯:下面是萬一有人在ASPX感興趣:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WriteMail.aspx.cs"  
Inherits="WriteMail" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
<style type="text/css"> 
    .style2 
    { 
     width: 203px; 
    } 
    .style3 
    { 
     width: 226px; 
    } 
    .style4 
    { 
    } 
    .style6 
    { 
     width: 198px; 
    } 
    .style7 
    { 
     width: 204px; 
    } 
    </style> 
    </head> 
    <body> 
    <form id="form1" runat="server"> 
    <div align="center" style="font-weight: bold; font-family: tahoma; font-size: 12px; 
    margin-bottom: 10px;"> 
    Hörmətli istifadəçi!</div> 
    <div align="center" style="font-family: tahoma; font-size: 12px;margin-bottom: 
    30px;"> 
    <span class="style2"><span class="style10">Azərbaycan Respublikası Dövlət Sosial 
    Müdafiə 
     Fonduna məktub göndərmək ücün aşağıdakı formanı doldurun.(* - vacib sahələr) 
    </span></span></div> 
    <div id="main"> 
    <table> 
     <tr> 
      <td class="style6"> 
       &nbsp;Adınız* :</td> 
      <td class="style7"> 
       Soyadınız* :</td> 
      <td class="style3"> 
       &nbsp;Atanızın adı*</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       <asp:TextBox ID="TextBox2" runat="server" Width="182px"></asp:TextBox> 
      </td> 
      <td class="style7"> 
       &nbsp;<asp:TextBox ID="TextBox3" runat="server" Width="182px"></asp:TextBox> 
      </td> 
      <td class="style3"> 
       &nbsp;<asp:TextBox ID="TextBox4" runat="server" Width="182px"></asp:TextBox> 
      </td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       &nbsp;</td> 
      <td class="style7"> 
       &nbsp;</td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       &nbsp;Ölkə*:&nbsp;</td> 
      <td class="style7"> 
       &nbsp;Şəhər*:&nbsp;</td> 
      <td class="style3"> 
       &nbsp;&nbsp; &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       <asp:TextBox ID="TextBox5" runat="server" Width="182px"></asp:TextBox> 
      </td> 
      <td class="style7"> 
       <asp:TextBox ID="TextBox6" runat="server" Width="182px"></asp:TextBox> 
      </td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       &nbsp;</td> 
      <td class="style7"> 
       &nbsp;</td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       &nbsp;Ünvan*:</td> 
      <td class="style7"> 
       &nbsp;</td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style4" colspan="3"> 
       <asp:TextBox ID="TextBox7" runat="server" Width="602px"></asp:TextBox> 
      </td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       &nbsp;</td> 
      <td class="style7"> 
       &nbsp;</td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       Telefon kodu və nömrəsi*:</td> 
      <td class="style7"> 
       Email*:</td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       <asp:TextBox ID="TextBox8" runat="server" Width="182px"></asp:TextBox> 
      </td> 
      <td class="style7"> 
       <asp:TextBox ID="TextBox9" runat="server" Width="182px"></asp:TextBox> 
      </td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       &nbsp;</td> 
      <td class="style7"> 
       &nbsp;</td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       Yaşınız*:</td> 
      <td class="style7"> 
       Cinsiniz*:</td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

     <tr> 
      <td class="style6"> 
       <asp:TextBox ID="TextBox10" runat="server" Width="66px"></asp:TextBox> 
      </td> 
      <td class="style7"> 
       <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
        RepeatDirection="Horizontal"> 
        <asp:ListItem Value="1">Kişi</asp:ListItem> 
        <asp:ListItem Value="2">Qadın</asp:ListItem> 
       </asp:CheckBoxList> 
      </td> 
      <td class="style3"> 
       &nbsp;</td> 
     </tr> 

    </table> 
</div> 
</form> 

+0

請提供aspx頁面的標記它表明你的控件是如何傳播出去。無論是在網格中還是在桌面上......類似的東西.. – 2012-04-26 07:32:23

回答

1

你可以使用下面的代碼獲取gett從你的aspx頁面獲取詳細信息並將其作爲電子郵件正文發送。

public string GetHtmlBody() 
{ 
    String strHTMLBody = String.Empty; 


    if (TextBox2.Text.ToString().Trim() != "") 
    { 
     strHTMLBody = strHTMLBody + "Name : " + TextBox2.Text.ToString().Trim() + "<br/>"; 
    } 
    if (TextBox3.Text.ToString().Trim() != "") 
    { 
     strHTMLBody = strHTMLBody + "Surname : " + TextBox3.Text.ToString().Trim() + "<br/>"; 
    } 
    if (TextBox2.Text.ToString().Trim() != "") 
    { 
     strHTMLBody = strHTMLBody + "Occupation : " + TextBox4.Text.ToString().Trim(); 
    } 

    // like wise you can get other details in same way in string variable. 

    return strHTMLBody; 

} 
public bool SendEmail(string to, string subject) 
{ 

    // get the hostname of the SMTP server from Web.config 
    string hostname = ConfigurationManager.AppSettings["SMTP"]; 
    // Example: add 
    //   to the section of Web.config 
    string user = ConfigurationManager.AppSettings["SMTP_user"]; 
    string pwd = ConfigurationManager.AppSettings["SMTP_pwd"]; 

    MailMessage mail = new MailMessage(); 
    mail.From = new MailAddress("[email protected]", "Name"); 
    mail.To.Add(to); 
    mail.Subject = subject; 
    mail.Body = GetHtmlBody(); 
    mail.IsBodyHtml = true ; 
    SmtpClient client = new SmtpClient(hostname); 
    client.Credentials = new System.Net.NetworkCredential(user, pwd); 
    try 
    { 
     client.Send(mail); 
     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 

} 

希望這將幫助你......快樂編碼....

2

這裏的C#代碼段從您的代碼發送電子郵件隱藏文件:

 
     public bool SendEmail(string to, string subject, string body) { 

      // get the hostname of the SMTP server from Web.config 
      string hostname = ConfigurationManager.AppSettings["SMTP"]; 
      // Example: add 
      //   to the section of Web.config 
      string user = ConfigurationManager.AppSettings["SMTP_user"]; 
      string pwd = ConfigurationManager.AppSettings["SMTP_pwd"]; 

      MailMessage mail = new MailMessage(); 
      mail.From = new MailAddress("[email protected]", "Name"); 
      mail.To.Add(to); 
      mail.Subject = subject; 
      mail.Body = body; 
      mail.IsBodyHtml = false; 
      SmtpClient client = new SmtpClient(hostname); 
      client.Credentials = new System.Net.NetworkCredential(user,pwd); 
      try { 
       client.Send(mail); 
       return true; 
      } catch (Exception) { 
       return false; 
      } 

     } 

好運與它;)

相關問題