位置 :  首页 / 博客 / 学无止境

ASP.NET发送邮件

发布人: 发布时间:2019/01/05 访问量:1474
发送邮件
Xiaobin.Utility.Tool.SendMail("邮件标题", "邮件内容", "收件人", "发件人", "发件用户名", "发件密码", "smtp.qq.com");
Utility.Tool.Alert("密码已经发送到您的邮箱中,请注意查收。", this.Page);

#region 发送email,默认是25端口
        /// <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>
        public static void 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);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
上一篇:ASP.NET上传文件