咨询电话:186 7916 6165 咨询电话:186 7916 6165 (微信同号)    在线QQ:181796286
NEWS BLOG ·
学无止境
关注开优网络 关注前沿
.NET 三种取整方法 向上取整 向下取整 四舍五入
C# string字符串的截取、移除、替换、插入

ASP.NET发送Emai

发表日期:2018-07-24    文章编辑:    浏览次数:243    标签:ASP.NET应用

1、前端.aspx


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>发送Email示例</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>发送Email示例</h1>
    <p>发件邮箱:<asp:TextBox ID="txtFrom" runat="server" Width="407px" Text="niunan@niunan.net"></asp:TextBox>
    </p>
    <p>用户名:<asp:TextBox ID="txtName" runat="server" Width="407px" Text="niunan@niunan.net"></asp:TextBox>
    </p>
    <p>密码:<asp:TextBox ID="txtPWD" runat="server" Width="407px" Text="nn13607886582"></asp:TextBox>
    </p>
    <p>SMTP地址:<asp:TextBox ID="txtSMTP" runat="server" Width="407px" Text="smtp.qq.com"></asp:TextBox>
    </p>
    <p>收件邮箱:<asp:TextBox ID="txtTo" runat="server" Width="407px" Text="164423073@qq.com"></asp:TextBox>
    </p>
    <p>标题:<asp:TextBox ID="txtTitle" runat="server" Width="407px" Text="test title"></asp:TextBox>
    </p>
    <p>内容:</p>
    <p>
        <asp:TextBox ID="txtContent" Text="test content" TextMode="MultiLine" runat="server" Height="125px" Width="432px"></asp:TextBox>
    </p>
    <p>
        <asp:Button ID="btnSend" runat="server" Text="发送" onclick="btnSend_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="lblMes" ForeColor="Red" runat="server" Text=""></asp:Label>
    </p>
    </form> 
</body>
</html>


2、后台.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 System.Net;

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

    }

    /// <summary>
    /// 发送email,默认是25端口
    /// </summary>
    /// <param name="title">邮件标题</param>
    /// <param name="body">邮件内容</param>
    /// <param name="toAdress">收件人</param>
    /// <param name="fromAdress">发件人</param>
    /// <param name="userName">发件用户名</param>
    /// <param name="userPwd">发件密码</param>
    /// <param name="smtpHost">smtp地址</param>
    private string sendMail(string title, string body, string toAdress, string fromAdress,
                          string userName, string userPwd, string smtpHost)
    {
        try
        {
            MailAddress to = new MailAddress(toAdress);
            MailAddress from = new MailAddress(fromAdress);
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
            message.IsBodyHtml = true; // 如果不加上这句那发送的邮件内容中有HTML会原样输出 
            message.Subject = title; message.Body = body;
            SmtpClient smtp = new SmtpClient();
            smtp.UseDefaultCredentials = true;
            smtp.Port = 25;
            smtp.Credentials = new NetworkCredential(userName, userPwd);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Host = smtpHost;
            message.To.Add(toAdress);
            smtp.Send(message);
            return "邮件发送成功!";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        string title = txtTitle.Text.Trim();
        string content = txtContent.Text.Trim();
        string to = txtTo.Text.Trim();
        string from = txtFrom.Text.Trim();
        string username = txtName.Text.Trim();
        string pwd = txtPWD.Text.Trim();
        string smtp = txtSMTP.Text.Trim();

        lblMes.Text = sendMail(title, content, to, from, username, pwd, smtp);
    }
}