2007-01-26
ASP.NET 2.0 發送電子郵件
1.
<configuration>
<!-把電子郵件設置添加到<system.net>元素-->
<system.net>
<mailSettings>
<smtp>
<network host="relayServerHostname" port="portNumber" userName="username" password="password" />
</smtp>
</mailSettings>
</system.net>
<system.web> ... </system.web>
</configuration>
2.
<table border="0">
<tr>
<td>
<b>Your Email:</b>
</td>
<td>
<asp:TextBox runat="server" ID="UsersEmail" Columns="30"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Subject:</b>
</td>
<td>
<asp:TextBox runat="server" ID="Subject" Columns="30">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<b>Body:</b> <br />
<asp:TextBox runat="server" ID="Body" TextMode="MultiLine" Columns="55" Rows="10">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button runat="server" ID="SendEmail" Text="Send Feedback" />
</td>
</tr>
</table>
3.
Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
'!!!把這個值更新為你的電子郵件地址
Const ToAddress As String = "you@youremail.com"
'(1)創建MailMessage實例
Dim mm As New MailMessage(UsersEmail.Text, ToAddress)
'(2)屬性MailMessage賦值
mm.Subject = Subject.Text
mm.Body = Body.Text
mm.IsBodyHtml = False
'(3)創建SmtpClient對像
Dim smtp As New SmtpClient
'(4)發送MailMessage(將使用Web.config設置)
smtp.Send(mm)
End Sub
<configuration>
<!-把電子郵件設置添加到<system.net>元素-->
<system.net>
<mailSettings>
<smtp>
<network host="relayServerHostname" port="portNumber" userName="username" password="password" />
</smtp>
</mailSettings>
</system.net>
<system.web> ... </system.web>
</configuration>
2.
<table border="0">
<tr>
<td>
<b>Your Email:</b>
</td>
<td>
<asp:TextBox runat="server" ID="UsersEmail" Columns="30"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Subject:</b>
</td>
<td>
<asp:TextBox runat="server" ID="Subject" Columns="30">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<b>Body:</b> <br />
<asp:TextBox runat="server" ID="Body" TextMode="MultiLine" Columns="55" Rows="10">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button runat="server" ID="SendEmail" Text="Send Feedback" />
</td>
</tr>
</table>
3.
Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
'!!!把這個值更新為你的電子郵件地址
Const ToAddress As String = "you@youremail.com"
'(1)創建MailMessage實例
Dim mm As New MailMessage(UsersEmail.Text, ToAddress)
'(2)屬性MailMessage賦值
mm.Subject = Subject.Text
mm.Body = Body.Text
mm.IsBodyHtml = False
'(3)創建SmtpClient對像
Dim smtp As New SmtpClient
'(4)發送MailMessage(將使用Web.config設置)
smtp.Send(mm)
End Sub