首页 » .NET » C#程序实现发送邮件

C#程序实现发送邮件

原文 http://blog.csdn.net/IT_rookie_newbie/article/details/77772603

2017-09-01 16:20:11阅读(544)

最近闲着无聊研究了一下发送邮件

具体代码如下:

      

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="email">收件人邮箱</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SendEmail(string email, string userName)
        {
            SendEmailHelper helper = new SendEmailHelper();
            //发件人邮箱
            helper.SendEmail = "*************@qq.com";
            //发件人姓名
            helper.SendName = "白码驿站(www.51baidu.com.cn)";
            //收件人邮箱
            helper.AddresseeEmail = email;
            //收件人姓名
            helper.AddresseeName = email;
            //标题
            helper.EmailTitle = "白码驿站(www.51baidu.com.cn)";
            //附件
            helper.File = "";
            //邮件内容
            helper.Content = "白码驿站(www.51baidu.com.cn)!";
            //邮箱服务器地址
            helper.SMTPHost = "smtp.qq.com";
            //发件人邮箱
            helper.SMTPuser = "*******************@qq.com";
            //发件人邮箱密码【授权密码】
            helper.SMTPpass = "*****************";
            bool b = helper.SendMail(helper);
            if (b)
            {
                //将验证码更新到数据库
                new FF_AccountService().UpdateVerification(str_r.ToLower(), userName);
            }
            return Json(b, JsonRequestBehavior.AllowGet);
        }


注:上面的 helper.SMTPpass中的密码不是qq密码是,开通服务是的授权码


SendEmailHelper类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
namespace AcmenDream.Helper
{
    public class SendEmailHelper
    {
        /// <summary>
        /// 发件人的邮箱
        /// </summary>
        public string SendEmail { get; set; }
        /// <summary>
        /// 发件人姓名
        /// </summary>
        public string SendName { get; set; }
        /// <summary>
        /// 收件人的邮箱
        /// </summary>
        public string AddresseeEmail { get; set; }
        /// <summary>
        /// 收件人姓名
        /// </summary>
        public string AddresseeName { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string EmailTitle { get; set; }
        /// <summary>
        /// 附件
        /// </summary>
        public string File { get; set; }
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 邮箱服务器地址
        /// </summary>
        public string SMTPHost { get; set; }
        /// <summary>
        /// 发件人邮箱
        /// </summary>
        public string SMTPuser { get; set; }
        /// <summary>
        /// 发件人邮箱密码
        /// </summary>
        public string SMTPpass { get; set; }
        /// <summary>
        /// 发送邮件方法
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public bool SendMail(SendEmailHelper model)
        {
            //设置from和to地址
            //发件人邮箱和收件人邮箱
            MailAddress from = new MailAddress(model.SendEmail, model.SendName);
            //发件人姓名 和收件人姓名
            MailAddress to = new MailAddress(model.AddresseeEmail, model.AddresseeName);
            //创建一个MailMessage对象
            MailMessage oMail = new MailMessage(from, to);
            // 添加附件
            if (model.File != "")
            {
                oMail.Attachments.Add(new Attachment(model.File));
            }
            //邮件标题
            oMail.Subject = model.EmailTitle;
            //邮件内容
            oMail.Body = model.Content;
            //邮件格式
            oMail.IsBodyHtml = false;
            //邮件采用的编码
            oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //设置邮件的优先级为高
            oMail.Priority = MailPriority.High;
            //发送邮件
            SmtpClient client = new SmtpClient();
            //client.UseDefaultCredentials = false;
            //服务器地址
            client.Host = model.SMTPHost;
            //账户密码
            client.Credentials = new NetworkCredential(model.SMTPuser, model.SMTPpass);
            //ssl加密
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                client.Send(oMail);
                return true;
            }
            catch (Exception err)
            {
                return false;
            }
            finally
            {
                //释放资源
                oMail.Dispose();
            }
        }
    }
}


注:发送邮件是需要开通服务(免费开通)的,就拿qq邮箱来举例子吧, 需要打开qq邮箱->设置->账户,找到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”


可以将几个服务全部开通.

经过简单的几步, 发送邮件程序就完成了!



转:http://51baidu.com.cn/Home/Search_List_Detail/120/4.html



最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹

天天可领!最高99块!土豪站长12月发红包啦!

关闭
支付宝红包