.net發送郵件
using System; using System.Collections.Generic; using System.Linq; using System.Net.Mail; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default6 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { bool state = SendMail("18703631269@163.com","songdongya","郵件gigade100標題","這個就是我發的郵件www.gigade100.com ","2394633278@qq.com"); Response.Write(state); } /// <summary> /// 发送邮件 /// </summary> /// <param name="MailFromAddress">发件人地址</param> /// <param name="MailFromUserName">用户名</param> /// <param name="MailFormPwd">密码</param> /// <param name="MailTitle">邮件标题</param> /// <param name="MailBody">邮件内容</param> /// <param name="MailTo">收件人地址</param> /// <returns>发送成功返回True否则返回False</returns> public static Boolean SendMail(string MailFromAddress, string MailFormPwd, string MailTitle, string MailBody, string MailTo) { try { MailMessage objMail = new MailMessage(); //发件人地址 objMail.From = new MailAddress(MailFromAddress); //邮件标题 objMail.Subject = MailTitle; //邮件标题编码 objMail.SubjectEncoding = System.Text.Encoding.UTF8; //邮件内容 objMail.Body = MailBody; //邮件内容编码 objMail.BodyEncoding = System.Text.Encoding.UTF8; //收件人地址在这里可以加多个 objMail.To.Add(MailTo); //用SMTP发送邮件的方式 SmtpClient client = new SmtpClient(); //用户名和密码 client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFormPwd); string StrMail = "163.com"; for (int i = 0; i < MailFromAddress.Length; i++) { if (MailFromAddress.Substring(i, 1).ToString().Trim() == "@") { StrMail = MailFromAddress.Substring(i + 1, MailFromAddress.Length - i - 1); } } //服务器名 client.Host = "smtp." + StrMail.ToString().Trim(); //发送 client.Send(objMail); return true; } catch (Exception) { return false; } } }
aspx頁面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html>
經過測試可以成功!
比較好的發送郵件