ASP.NET使用SmtpMail发送邮件的实例
下面C#源码将使用smtpmail发送邮件示例:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html>
<head>
<title>email</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>
<font face="宋体">收件人:
<asp:TextBox ID="tbTo" runat="server" Width="232px"></asp:TextBox></font></p>
<p>
<font face="宋体">主题:
<asp:TextBox ID="tbSubject" runat="server" Width="248px"></asp:TextBox></font></p>
<p>
<font face="宋体">内容:
<asp:TextBox ID="tbBody" runat="server" TextMode="MultiLine" Width="256px" Height="128px"></asp:TextBox></font></p>
<p>
<asp:Button ID="Button1" runat="server" Text="发送" OnClick="Button1_Click"></asp:Button></p>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
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 System.Web.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage myMail = new MailMessage();
myMail.Subject = this.tbSubject.Text.Trim();
myMail.From = "dam886@sina.com";
myMail.To = this.tbTo.Text.Trim();
myMail.Body = this.tbBody.Text;
SmtpMail.SmtpServer = "smtp.sina.com";
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "dam886");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "ytitsjh");
SmtpMail.Send(myMail);
Response.Write("OK");
}
}
要开通邮件的
pop3 和smtp协议