zoukankan      html  css  js  c++  java
  • C#发送邮件

    前台代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
           请输入您的邮箱:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="发送验证码" /><br /><br />
            请输入验证码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button2" runat="server" Text="验证" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.Net.Mail;
    
    public partial class Default1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
            Button2.Click += Button2_Click;
        }
    
        void Button2_Click(object sender, EventArgs e)
        {
            if (TextBox2.Text.Trim() == Session["YZM"].ToString())
            {
                Label1.Text = "OK";
            }
            else
            {
                Label1.Text = "Error";
            }
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            //创建SMTP调用服务类
            SmtpClient smtp = new SmtpClient("smtp.sina.cn");
    
            //创建发送人对象
            MailAddress aaa = new MailAddress("18560812711@sina.cn");
            //创建接收人对象
            MailAddress to = new MailAddress(TextBox1.Text);
            //创建邮件对象
            MailMessage mail = new MailMessage(aaa,to);
            //填充邮件主题
            mail.Subject="来自于起航科技用户注册的验证码邮件";
            //填充邮件内容
            mail.Body="您的验证码为[1234],请在20分钟内填写,此邮件为系统邮件,勿回复!";
            Session["YZM"] = "1234";
            //注册证书,验证发送人邮箱和密码
            NetworkCredential net = new NetworkCredential("18560812711@sina.cn", "hq1234561");
            //将证书关联到服务器对象等待验证
            smtp.Credentials = net;
    
            //调用发送方法
            smtp.Send(mail);
        }
    }
  • 相关阅读:
    apk应用签名获取
    git强制推送命令
    Maven 将本地jar包添加到本地仓库
    关于Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin的解决方案
    启动React-Native项目步骤
    Git初始化本地项目并提交远程仓库基础操作
    You have not accepted the license agreements of the following SDK components: [Android SDK Build-Tools 26.0.1, Android SDK Platform 26]
    kenkins安装
    Linux关闭防火墙和SELinux
    Linux下nginx安装配置
  • 原文地址:https://www.cnblogs.com/123lucy/p/5815674.html
Copyright © 2011-2022 走看看