AspNet Core 2.2 SendGrid邮件发送(可群发)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wanglui1990/article/details/85256189

开发环境:ASPNet Core 2.2
步骤:
1,前往SendGrid官网,申请账号。有免费账号,30天无限制,30天后仍可使用,做测试足够。
2,新建类库
2,安装包:PM> Install-Package SendGrid
3,实现:
a),邮件默认配置

    /// <summary>
    /// EmailSender默认配置
    /// </summary>
    public class EmailOptions
    {
        public string SendGridKey { get; set; } = "你申请到的apikey";        
        public EmailAddress SendGridEmailFrom { get; set; } = new EmailAddress("admin@localhost.com", "admin");//邮件默认发送人与显示名称
    }

b),邮件单发与群发

    /// <summary>
    /// IEmailSender扩展
    /// </summary>
    public interface IEmailSenderExtension:IEmailSender
    {
        Task SendMultiEmailAsync(List<string> emails, string subject, string htmlMessage);
    }
    public class EmailSender : IEmailSenderExtension
    {
        public EmailSender(IOptions<EmailOptions> optionsAccessor)
        {
            Options = optionsAccessor.Value;
        }

        public EmailOptions Options { get; }

        /// <summary>
        /// 邮件发送:单个收件人
        /// </summary>
        /// <param name="email">收件人邮箱</param>
        /// <param name="subject">主题</param>
        /// <param name="message">内容</param>
        /// <returns></returns>
        public Task SendEmailAsync(string email, string subject, string message)
        {
            var client = new SendGridClient(Options.SendGridKey);
            var msg = new SendGridMessage()
            {
                From = Options.SendGridEmailFrom,
                Subject = subject,
                PlainTextContent = message,
                HtmlContent = message
            };
            msg.AddTo(new EmailAddress(email));

            var resp = client.SendEmailAsync(msg);
            return resp;
        }

        /// <summary>
        /// 邮件发送:多个收件人
        /// </summary>
        /// <param name="emails">收件人邮箱列表</param>
        /// <param name="subject">主题</param>
        /// <param name="message">内容</param>
        /// <returns></returns>
        public Task SendMultiEmailAsync(List<string> emails, string subject, string message)
        {
            var client = new SendGridClient(Options.SendGridKey);
            var msg = new SendGridMessage()
            {
                From = Options.SendGridEmailFrom,
                Subject = subject,
                PlainTextContent = message,
                HtmlContent = message
            };
            msg.AddTos(emails.Select(email => { return new EmailAddress(email); }).ToList());
            var resp = client.SendEmailAsync(msg);
            return resp;
        }
    }

4,新建web应用,测试邮件发送
关键点:
Startup中注册

services.AddTransient<SendGridLib.IEmailSenderExtension, SendGridLib.EmailSender>();

5,Home控制器中测试代码:
测试发现 不支持QQ邮箱。
有时收邮件会有延迟。

    public class HomeController : Controller
    {
        private readonly IEmailSenderExtension _emailSender;
        public HomeController(IEmailSenderExtension emailSender)
        {
            _emailSender = emailSender;
        }
        public async Task<IActionResult> Index()
        {
            await _emailSender.SendEmailAsync("xx1@163.com", "主题aaa", "单邮件测试");
            await _emailSender.SendMultiEmailAsync(new List<string>() { "xx1@163.com",  "xx2@gmail.com" }, "主题aaa多人", "多邮件测试");
            return View();
        }
    }

源码示例下载链接

展开阅读全文

后台邮件发送错误问题(含群发/单发)

10-29

错误提示:rn服务器对象 错误 'ASP 0177 : 800401f3' rnrnServer.CreateObject 失败 rnrn/down/manage/sendmailto.asp,行102 rnrn无效的 ProgID。 若要获取关于此消息的更多的信息,请访问 Microsoft 联机支持站点: http://www.microsoft.com/contentredirect.asp 。 rnrnrnrn相关源码:rn<%@language=vbscript codepage=936 %>rnrnrnrn rn rn rn rn rn rn 功能菜单rn rn rn rn rn rn rn rn 邮件列表管理rn rn rn rn rn rn 邮件列表设置rn rn rn rn rn rn 发送邮件rn rn rn rn rn rn 用户管理rn rn rn rn rn rn 添加用户rn rn rn rn rn rn rn rn rn rn rn rn rn rn 发 送 邮 件 rn rn rn <% rn'发送rnset rs=server.createobject("adodb.recordset") rnsql="select * from email "rnrs.open sql,conn,1,3 rnrn'读取默认的邮件标题及内容 rnset rs1=server.createobject("adodb.recordset")rnsql1="select * from maildefault "rnrs1.open sql1,conn,1,3 rnrn'设置发信人rnfrommail=request("frommail")rnif frommail="" thenrnfrommail=rs1("frommail")rnend ifrnrn'设置邮件主题rnmailsubject=request("mailsubject")rnif mailsubject="" thenrnmailsubject=rs1("mailsubject")rnend ifrnrn'设置邮件内容rnmailbody=request("mailbody")rnif mailbody="" thenrnmailbody=rs1("mailbody")rnend ifrnrn'判断对谁发信rntomail=request("tomail")rn'写发信信息rnresponse.write "发信人地址: "&frommailrnresponse.write " "rnif tomail<>"" thenrnresponse.write "收信人地址:"&tomailrnelsernresponse.write "正在进行邮件群发!"rnend ifrnrnif tomail<>"" thenrn'对于单一用户发信rnSet objCDOMail = Server.CreateObject("CDONTS.NewMail")rnobjCDOMail.From = frommailrnobjCDOMail.To = tomailrnobjCDOMail.Subject = mailsubject rnobjCDOMail.Body = mailbody rnobjCDOMail.SendrnSet objCDOMail = Nothingrnelsernrn'对于在用户数据库中的全体用户发信rnfor i=1 to rs.recordcountrntomail=rs("email")rnSet objCDOMail = Server.CreateObject("CDONTS.NewMail")rnobjCDOMail.From = frommailrnobjCDOMail.To = tomailrnobjCDOMail.Subject = mailsubject rnobjCDOMail.Body = mailbody rnobjCDOMail.SendrnSet objCDOMail = Nothingrnrs.movenextrnnextrnend ifrnresponse.write " "rnresponse.write "邮件发送成功!^&^"rn'response.write " "rn'response.write rs1("mailsubject")rn%> 返回rn rn rnrn 论坛

没有更多推荐了,返回首页