C#用System.Net.Mail实现邮件发送功能

本文详细介绍如何利用System.Net.Mail库在ASP.NET中实现邮件发送功能。通过前端页面收集SMTP服务器、邮箱账户、密码等信息,并在后台使用C#代码进行邮件发送。文章提供了完整的代码示例,包括错误处理和SSL加密选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用System.Net.Mail实现邮件发送功能其实很简单,话不多说,直接上代码
前端aspx页面:

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>自己给自己发邮件</h2>
        <table border="1">
            <tr>
                <td><asp:Label ID="lb_SmtpServer" runat="server" Text="Smtp Server:"></asp:Label></td>
                <td><asp:TextBox ID="SmtpServer" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:Label ID="lb_EmailAddress" runat="server" Text="Email Adress:"></asp:Label></td>
                <td><asp:TextBox ID="emailaddress" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:Label ID="lb_UserName" runat="server" Text="User Name:"></asp:Label></td>
                <td><asp:TextBox ID="username" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:Label ID="lb_Password" runat="server" Text="Password:"></asp:Label></td>
                <td><asp:TextBox ID="password" textmode="password" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:Label ID="lb_Port" runat="server" Text="Port:"></asp:Label></td>
                <td><asp:TextBox ID="port" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:CheckBox Text="Using SSL?" ID="SSL" runat="Server"/></td>
                <td><asp:CheckBox Text="Anonymous" ID="Anonymous" runat="server" /></td>
            </tr>
            <tr>
                <td></td>
                <td><asp:Button  ID="test" runat="server" Text="Test" OnClick="BT_test_Click"/></td>
            </tr>
        </table>
    </div>
    <div>
        <h2>给别人发</h2>
        <asp:Label runat="server">请输入收件人邮箱(多个以;隔开)</asp:Label>
        <asp:TextBox ID="receveraddr" runat="server" Height="100px" Width="300px"></asp:TextBox>
        <asp:Button ID="send" runat="server" Text="Send" OnClick="BT_test_Click" />
    </div>
    </form>
</body>

后台对应的cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using Newtonsoft.Json;

namespace Email
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void BT_test_Click(object sender, EventArgs e) {
        	//获取SmtpServer、你邮箱用户名、你邮箱授权码、端口port、你邮箱地址、收件人地址
            string smtp = this.SmtpServer.Text;
            string username = this.username.Text;
            string password = this.password.Text;
            string port = this.port.Text;
            string emailaddress = this.emailaddress.Text;
            string receveraddr = this.receveraddr.Text;
    		if (smtp == null || smtp.Length == 0)
            {
                Response.Write("<Script>alert('smtp is null');</Script>");
                 return;
            }
            if(this.Anonymous.Checked==false){
                if (username == null || username.Length == 0)
                {
                    Response.Write("<Script>alert('username is null');</Script>");
                    return;
                }
                if (password == null || password.Length == 0)
                {
                    Response.Write("<Script>alert('password is null');</Script>");
                    return;
                }
            }
            int smtpport = 0;
            try
            {
                if (port.Length > 0 && port != "0")
                {
                    smtpport = Convert.ToInt32(port);
                }
                else {
                    this.port.Text = "25";
                    smtpport = 25;
                }
            }
            catch
            {
                this.port.Text = "25";
                smtpport = 25;
            }
            EmailInfo emailInfo = null;
            if (receveraddr == null || receveraddr.Length == 0)
            {
                 emailInfo = new EmailInfo(smtp, username, password, emailaddress, emailaddress, "Test", "This is a test eamil", null);
            }
            else {
                emailInfo = new EmailInfo(smtp, username, password, emailaddress, receveraddr, "Test", "This is a test eamil", null);
            }
            //判断是否选择SSL加密服务
            bool SSL = this.SSL.Checked;
            //是否匿名发送(不需要你输入用户名及密码)
            bool Anonymous = this.Anonymous.Checked;
            string result=SendEmail(emailInfo, smtpport,SSL,Anonymous);
            Response.Write("<Script>alert('"+result+"');</Script>");
        }
        public string SendEmail(Object emailInfo, int port, bool UsingSSL, bool Anonymous)
        {
            MailMessage message = null;
            try {
                EmailInfo emailinfo = (EmailInfo)emailInfo;
                SmtpClient client = new SmtpClient(emailinfo.Smtpserver);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = UsingSSL;
                if(!client.EnableSsl){
                    if(port!=0){
                        client.Port = port;
                    }
                }
                MailAddress from = new MailAddress(emailinfo.From);
                message = new MailMessage();
                //发件人
                message.From = from;
                //收件人(一个或多个)
                string[] recaddress;
                if (emailinfo.To.IndexOf(";") > -1)
                {
                    recaddress = emailinfo.To.Split(';');
                    foreach (string addr in recaddress)
                    {
                        if (addr != "")
                        {
                            message.To.Add(addr);
                        }
                    }
                }
                else {
                    message.To.Add(emailinfo.To.Trim());
                }
                //邮件主题
                message.Subject = emailinfo.Subject;
                //主题编码格式
                message.SubjectEncoding = System.Text.Encoding.ASCII;
                //邮件内容
                message.Body = emailinfo.Body;
                message.BodyEncoding = System.Text.Encoding.ASCII;
                if (Anonymous == false)
                {
                    client.Credentials = new System.Net.NetworkCredential(emailinfo.Username, emailinfo.Password);
                }
                client.Send(message);
                return "Send Success";
            }
            catch {
                return "Send Faild";
            }
        }

        public  class EmailInfo { 
            public  string Smtpserver;//SmtpServer
            public  string Username;//账号
            public  string Password;//密码
            public  string From;//发件人
            public  string To;//收件人
            public  string Subject;//主题
            public  string Body;//内容
            public  string Attach;//附件

            public  EmailInfo(string smtpserver,string username,string password,string from,string to,string subject,string body,string attach) {
                Smtpserver = smtpserver;
                Username = username;
                Password = password;
                From = from;
                To = to;
                Subject = subject;
                Body = body;
                Attach = attach;
            }
        }
    }
}

这是第三方登录邮箱,需要在邮箱设置里面开启Smtp服务并获取授权码(第三方登录邮箱的密码),否则会报错
戳我下载源码
戳我下载源码
戳我下载源码
戳我下载源码
戳我下载源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值