asp.net发送邮件

<%@ 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协议

posted @ 2009-10-24 17:21  IT老农  阅读(1163)  评论(6编辑  收藏  举报