前臺:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form runat="server" action="">
- <asp:Button ID="Button1" runat="server" Text="發送" OnClick="Button1_Click" />
- </form>
- </body>
- </html>
後臺:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using AjaxControlToolkit;
- using System.Web.Services;
- using System.Data.SqlClient;
- using System.Net;
- using System.Net.Mail;
- using System.Text;
- public partial class nameSpace08108_Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- try
- {
- MailAddress mailFrom = new MailAddress("****@126.com");
- MailAddress mailTo = new MailAddress("****@126.com");
- MailMessage objMail = new MailMessage(mailFrom, mailTo);
- objMail.IsBodyHtml = false;
- objMail.Priority = MailPriority.High;
- objMail.Subject = "測試";
- objMail.BodyEncoding = Encoding.UTF8;
- objMail.Body = "你好!這是一個關於發送郵件的測試...";
- SmtpClient client = new SmtpClient("smtp.126.com");
- client.Credentials = new System.Net.NetworkCredential("用戶名", "密碼");
- client.Send(objMail);
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- }