.netcore中发送邮件

        public void test([FromBody] Class1 class1)
        {
            MailMessage mailMsg = new MailMessage();//实例化对象
            mailMsg.From = new MailAddress("[email protected]", "looper.m.zhuo");//源邮 
            地址和发件人
            mailMsg.To.Add(new MailAddress("[email protected]"));//收件人地址
            mailMsg.Subject = "邮件发送";//发送邮件的标题
            StringBuilder sb = new StringBuilder();
            sb.Append(".netcore 发测试邮件");
            mailMsg.Body = sb.ToString();//发送邮件的内容
            //指定smtp服务地址(根据发件人邮箱指定对应SMTP服务器地址)
            SmtpClient client = new SmtpClient();//格式:smtp.126.com  smtp.164.com
            client.Host = "smtp.qq.com";
            //要用587端口
            client.Port = 587;//端口
            //加密
            client.EnableSsl = true;
            //通过用户名和密码验证发件人身份
            client.Credentials = new NetworkCredential("[email protected]", 
            "xplrcvscipkkebaj"); // 
            //发送邮件
            try
            {
                client.Send(mailMsg);
            }
            catch (SmtpException ex)
            {
                Console.WriteLine(ex.Message);

            }
            Console.WriteLine("邮件已发送,请注意查收!");
            Console.ReadKey();
        }

猜你喜欢

转载自blog.csdn.net/qq_40677590/article/details/104061152