C#后台发送邮件

        public IActionResult Send()
        {
            string address = "smtp.163.com";
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(address);
            //生成一个发送地址
            string strFrom = "f17623079805@163.com";

            //构造一个发件人地址对象
            MailAddress from = new MailAddress(strFrom, "SendName", Encoding.UTF8);
            //构造一个收件人地址对象
            MailAddress to = new MailAddress("857944124@qq.com","receiverName", Encoding.UTF8);

            //构造一个Email的Message对象
            MailMessage message = new MailMessage(from, to);

            为 message 添加附件
            //foreach (TreeNode treeNode in treeViewFileList.Nodes)
            //{
            //    //得到文件名
            //    string fileName = treeNode.Text;
            //    //判断文件是否存在
            //    if (File.Exists(fileName))
            //    {
            //        //构造一个附件对象
            //        Attachment attach = new Attachment(fileName);
            //        //得到文件的信息
            //        ContentDisposition disposition = attach.ContentDisposition;
            //        disposition.CreationDate = System.IO.File.GetCreationTime(fileName);
            //        disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileName);
            //        disposition.ReadDate = System.IO.File.GetLastAccessTime(fileName);
            //        //向邮件添加附件
            //        message.Attachments.Add(attach);
            //    }
            //    else
            //    {
            //        MessageBox.Show("文件" + fileName + "未找到!");
            //    }
            //}

            //添加邮件主题和内容
            message.Subject = "FirstTest";
            message.SubjectEncoding = Encoding.UTF8;
            message.Body = "我爱你.................................................中国";
            message.BodyEncoding = Encoding.UTF8;

            //设置邮件的信息
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = false;

            //如果服务器支持安全连接,则将安全连接设为true。
            //gmail支持,163不支持,如果是gmail则一定要将其设为true
            //if (cmbBoxSMTP.SelectedText == "smpt.163.com")
                client.EnableSsl = true;
          

            //设置用户名和密码。
            //string userState = message.Subject;
            client.UseDefaultCredentials = false;
            string username = "f17623079805";
            string passwd = "申请的邮箱开通smtp的密码";
            //用户登陆信息
            NetworkCredential myCredentials = new NetworkCredential(username+"@163.com", passwd);
            client.Credentials = myCredentials;
            //发送邮件
           client.Send(message);
            //提示发送成功
        
            return View();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值