其他分享
首页 > 其他分享> > .NET邮箱发送功能

.NET邮箱发送功能

作者:互联网

具体文字介绍,请查看https://www.cnblogs.com/scode2/p/8810244.html

下面是我实现邮箱发送功能的代码:

//需要引入的动态库
using System;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace 命名空间.TestDemo
{
    public class SendMailApp
    {
        /// <summary>
        /// 发送邮箱给用户,提供找回密码的服务
        /// </summary>
        /// <param name="sendEmailHost">邮箱服务地址(默认的为网易云邮箱)</param>
        /// <param name="sendEmailHost">收件人的邮箱地址</param>
        /// <param name="userName">用户姓名</param>
        /// <param name="pwd">网站登录的密码</param>
        /// <param name="recipientEmail">接收邮件的邮箱地址</param>
        /// <returns>string</returns>
        public object sendEmail(string sendEmailHost, string userEmail, string myEmail, string myEmailPwd, string userName, string pwd, string recipientEmail)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(myEmail);//发送人邮箱地址
            //mailMessage.To.Add("1831412916@qq.com");//收件人的邮箱地址
            mailMessage.To.Add(userEmail);//收件人的邮箱地址
            mailMessage.Subject = "您好!" + userName + "先生/女士:";//邮箱主题
            mailMessage.Body = "您的登录密码是:" + pwd;//正文
            mailMessage.IsBodyHtml = true;//正文内容是否为html
            mailMessage.SubjectEncoding = Encoding.UTF8;
            mailMessage.Priority = MailPriority.High;
            mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = sendEmailHost;//邮箱服务器的地址
            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = false; //ssl加密 
            smtp.Port = 25;
            smtp.Timeout = 60000;
            smtp.Credentials = new NetworkCredential(myEmail, myEmailPwd);
            try
            {
                smtp.Send(mailMessage);
                return new { state = "success", message = "邮件已发送至您“" + userEmail + "”的邮箱!请注意查收。" };
            }
            catch (Exception e)
            {
                return new { state = "sendError", message = "邮件发送失败!" };
            }
        }
    }
}

代码就这么多咯!回见

标签:string,邮箱地址,mailMessage,smtp,发送,new,邮箱,NET
来源: https://blog.csdn.net/qq_42773229/article/details/88061625