.Net中通过需要认证的SMTP服务器发送邮件

 在系统目录下的system32文件夹里有一个cdosys.dll文件,我们可以通过.Net调用它,达到通过需要验证的SMTP服务器(如smtp.163.com)发送邮件的目的。

具体方法:

在VS2005的解决方案资源管理器中,添加引用cdosys.dll,添加新项mail.aspx,加入3个TextBox,分别是:收件人地址、主题、正文内容。

Mail.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Mail.aspx.cs" Inherits="Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="500">
            <tr>
                <td style="width: 171px; height: 30px">
                    收信人</td>
                <td colspan="2" style="width: 500px; height: 30px">
                    <asp:TextBox ID="TextBox1" runat="server" Width="219px"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
                        ErrorMessage="*s" ValidationExpression="/w+([-+.']/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*"></asp:RegularExpressionValidator></td>
            </tr>
            <tr>
                <td style="width: 171px; height: 30px">
                    主题</td>
                <td colspan="2" style="width: 500px; height: 30px">
                    <asp:TextBox ID="TextBox2" runat="server" Width="218px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
                        ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td style="width: 171px">
                    正文</td>
                <td colspan="2" style="width: 500px">
                    <asp:TextBox ID="TextBox3" runat="server" Height="179px" TextMode="MultiLine" Width="396px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox3"
                        ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td align="center" colspan="3">
                    <asp:Button ID="Button1" runat="server" Height="22px" OnClick="Button1_Click" Text="发送"
                        Width="70px" /></td>
            </tr>
        </table>
     </div>
    </form>
</body>
</html>

Mail.aspx.cs代码如下:
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;

public partial class Mail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
   }
    public void cdosendmail()
    {
        try
        {
            CDO.Message msg = new CDO.Message();
            msg.From = "用户名@163.com";
            msg.To = TextBox1.Text.ToString().Trim();
            msg.Subject = TextBox2.Text.ToString().Trim();
            msg.HTMLBody = "<html><body>" + TextBox3.Text.ToString().Trim() + "</body></html>";
            CDO.IConfiguration config = msg.Configuration;
            ADODB.Fields ofields = config.Fields;
            ofields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
            ofields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "用户名";
            ofields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "密码";
            ofields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
            ofields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
            ofields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.163.com";
           //可以换成别的,跟"msg.From"里的地址对应就可以了
            ofields.Update();

            msg.BodyPart.Charset = "gb2312";
            msg.HTMLBodyPart.Charset = "gb2312";

            msg.Send();
            msg = null;
            Response.Write("<script>alert('发送成功:)');</script>");
        }
        catch
        {
            Response.Write("<script>alert('发送失败:(');</script>");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        this.cdosendmail();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值