ASP.net code C# การใช้งาน Mail ในการทำหน้า Contact Us
posted on 30 Oct 2009 15:00 by techinnoreview in MITT// ส่วน code
public partial class Mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e) // ปุ่มส่ง
{
Label1.Text = "<br/>" + TextBox1.Text + "<br/>";
Label1.Text += "<br/>" + TextBox2.Text + "<br/>";
Label1.Text += "<br/>" + TextBox3.Text + "<br/>" + "\n";
Label1.Text += "<br/>" + "<b>All massage been sent<b/>" + "<br/>";
// โชว์ข้อความหลังกดส่ง
MailMessage mail = new MailMessage();
mail.From = new MailAddress(TextBox2.Text, TextBox1.Text);
mail.To.Add(new MailAddress("zinazisc@gmail.com", "Admin"));
mail.Subject = "Message from:" + TextBox1.Text;
string body;
body = TextBox1.Text + " Said " + "\n" + TextBox3.Text + "\n" + "Sent At : " + DateTime.Now.ToString();
mail.Body = body;
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
// ส่วนที่ใช้ในการส่งอีเมล์
}
protected void Button2_Click(object sender, EventArgs e) // ปุ่มเคลีย
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
// ใส่ค่าว่าง
}
}
// ส่วนหน้าเว็บ
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Mail</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 53px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
Mail</div>
<p>
Contact Us</p>
<p>
<table class="style1">
<tr>
<td class="style2">
Name:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Please Complete This Field"
ValidationGroup="1"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
Email: </td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" Display="Dynamic"
ErrorMessage="Please Complete This Field" ValidationGroup="1"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox2" Display="Dynamic"
ErrorMessage="Email Format Wrong"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="1"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td align="left" class="style2" valign="top">
Message:
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Height="81px" TextMode="MultiLine"
Width="227px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Please Complete This Field"
ValidationGroup="1"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send"
ValidationGroup="1" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
onclick="Button2_Click" style="margin-left: 78px" Text="Clear" />
</p>
<p>
Detail
</p>
<p>
<asp:Label ID="Label1" runat="server"></asp:Label>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
</form>
</body>
</html>
// ส่วน webconfig ซึ่งเพิ่มเข้ามาหลัง </system.web>
<system.net>
<mailSettings>
<smtp from ="admin@simplydev.net">
<network host="mail.simplydev.net" userName="info@simplydev.net" password="****" port="25"/> // password พวกนี้ต้องหาตั้งและใส่้เอาเองนะครับ
</smtp>
</mailSettings>
</system.net>
Tags: asp, code, contact, net, system, us, web, เขียนเว็บ, เขียนโปรแกรม0 Comments