zoukankan      html  css  js  c++  java
  • ASP.NET 发送email

    首先添加命名空间
    using System.Net.Mail;
    
      /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void ForMail(string name, string mail)
            {
                try
                {
                    //string email = txtemail.Text.Trim();
                    MailAddress MessageFrom = new MailAddress("【发件箱地址】"); //发件人邮箱地址
                    string MessageTo = mail; //收件人邮箱地址
                    string MessageSubject = bs.HtmlEncode(web_zt.Value.Trim()); //邮件主题  
                    string MessageBody = "" + content1.Value.Trim() + ""; //邮件内容 (一般是一个网址链接,生成随机数加验证id参数,点击去网站验证。)";
                    if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody))
                    {
                        //Response.Write("发送邮件成功");
                    }
                    else
                    {
                        //Response.Write("发送邮件失败");
                    }
                }
                catch
                {
                    //ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('客户信息删除失败')</script>");
                }
            }
            /// <summary>
            /// 发送电子邮件
            /// </summary>
            /// <param name="MessageFrom">发件人邮箱地址</param>
            /// <param name="MessageTo">收件人邮箱地址</param>
            /// <param name="MessageSubject">邮件主题</param>
            /// <param name="MessageBody">邮件内容</param>
            /// <returns></returns>
            public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
            {
                MailMessage message = new MailMessage();
                message.From = MessageFrom;
                message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发
                message.Subject = MessageSubject;
                message.Body = MessageBody;
                message.IsBodyHtml = true; //是否为html格式
                message.Priority = MailPriority.Normal; //发送邮件的优先等级
                SmtpClient sc = new SmtpClient();
                sc.Host = "smtp.qq.com"; //指定发送邮件的服务器地址或IP
                sc.Port = 25; //指定发送邮件端口
                sc.Credentials = new System.Net.NetworkCredential("【发件箱地址】", "【发件箱密码】"); //指定登录服务器的用户名和密码(发件人的邮箱登陆密码)
                try
                {
                    sc.Send(message); //发送邮件
                }
                catch
                {
                    return false;
                }
                return true;
            }
  • 相关阅读:
    ACM学习历程—HDU4415 Assassin’s Creed(贪心)
    ACM学习历程—HDU4417 Super Mario(树状数组 && 离线)
    ACM学习历程—HDU5423 Rikka with Tree(搜索)
    ACM学习历程—HDU5422 Rikka with Graph(贪心)
    ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)
    ACM学习历程—ZOJ3785 What day is that day?(数论)
    ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)
    ACM学习历程—HDU5407 CRB and Candies(数论)
    日志-logback
    使用特定功能代码
  • 原文地址:https://www.cnblogs.com/houlin/p/3512472.html
Copyright © 2011-2022 走看看