• C# 两种方式发送邮件 MailTo + SMTP


    通过mailto标签和SMTP协议两种方式发送邮件

    using System;
    using System.Collections.Generic;
    using System.Net.Mail;
    using System.Text;
    
    namespace WUtility.ToolHelper
    {
        public class EmailHelper
        {
            //格式:mailto:发件人地址
            //格式:mailto:发件人地址?subject=邮件主题
            //格式:mailto:发件人地址?cc=抄送地址&subject=邮件主题
            //格式:mailto:发件人地址?bcc=密送地址
            //格式:mailto:发件人地址?body=邮件内容
            //格式:mailto:发件人地址1,发件地址2,发件地址3(以逗号分隔)   [以逗号分隔可能邮箱不识别(稍等一会,或者在邮箱中点击下),可以分号分隔]
            //格式:mailto:发件人地址1,发件人地址2?cc=抄送地址&bcc=密送地址&subject=主题&body=邮件内容
    
            //body中%0a%0d用于换行(只用一个就可以)
            //e.x.  ******%0a%0d******
            //输出:******
            //      ******
    
            /// <summary>
            /// 调用默认工具发送Email
            /// </summary>
            /// <param name="sendTo">接收人,可多个以逗号分隔</param>
            /// <param name="cc">抄送人</param>
            /// <param name="bcc">密送地址</param>
            /// <param name="subject">主题</param>
            /// <param name="body">内容</param>
            public static void DefaultSendMailTo(string sendTo = " ", string cc = " ", string bcc = " ", string subject = " ", string body = " ")
            {
                string message = string.Format("mailto:{0}?cc={1}&bcc={2}&subject={3}&body={4}", sendTo ?? " ", cc ?? " ", bcc ?? " ", subject ?? " ", body ?? " ");
                System.Diagnostics.Process.Start(message);//调用进程启动邮件
            }
    
            /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="userEmailAddress">发件人地址</param>
            /// <param name="userName">发件人姓名(可为空)</param>
            /// <param name="password">密码</param>
            /// <param name="host">邮件服务器地址</param>
            /// <param name="port"></param>
            /// <param name="sendToList">收件人(多个电子邮件地址之间必须用逗号字符(“,”)分隔)</param>
            /// <param name="sendCCList">抄送人(多个电子邮件地址之间必须用逗号字符(“,”)分隔)</param>
            /// <param name="subject">主题</param>
            /// <param name="body">内容</param>
            /// <param name="attachmentsPath">附件路径</param>
            /// <param name="errorMessage">错误信息</param>
            public static bool SendMessage(string userEmailAddress, string userName, string password, string host, int port,
                string[] sendToList, string[] sendCCList, string subject, string body, string[] attachmentsPath, out string errorMessage)
            {
                errorMessage = string.Empty;
                SmtpClient client = new SmtpClient();
    
                //注意:一定要先设置 EnableSsl和UseDefaultCredentials,再实例化Credentials 
                client.EnableSsl = false;
                client.UseDefaultCredentials = true;
    
                client.Credentials = new System.Net.NetworkCredential(userEmailAddress, password);//用户名、密码
                client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                client.Host = host;//邮件服务器
                client.Port = port;//端口号 非SSL方式,默认端口号为:25
    
                MailMessage msg = new MailMessage();
    
                //加发件人
                foreach (string send in sendToList)
                {
                    msg.To.Add(send);
                }
                //加抄送
                if (sendCCList != null)
                {
                    foreach (string cc in sendCCList)
                    {
                        msg.To.Add(cc);
                    }
                }
    
                //在有附件的情况下添加附件
                if (attachmentsPath != null && attachmentsPath.Length > 0)
                {
                    foreach (string path in attachmentsPath)
                    {
                        var attachFile = new Attachment(path);
                        msg.Attachments.Add(attachFile);
                    }
                }
                msg.From = new MailAddress(userEmailAddress, userName);//发件人地址
                msg.Subject = subject;//邮件标题
                msg.Body = body;//邮件内容
                msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
                msg.IsBodyHtml = true;//是否是HTML邮件
                msg.Priority = MailPriority.High;//邮件优先级
    
                try
                {
                    client.Send(msg);
                    return true;
                }
                catch (System.Net.Mail.SmtpException ex)
                {
                    errorMessage = ex.Message;
                    return false;
                }
            }
    
        }
    }

    使用示例:

            private void button16_Click(object sender, EventArgs e)
            {
                string szSendTo = "1234@qq.com;12345@qq.com";
                string szCC = "123456@163.com;1234567@qq.com";
                string szBcc = "12345678@qq.com";
                string szSubject = "测试主题";
                string body = "******今天天气不错哦,%0a%0d";
                body += "据说明天天气也不错哦";
                EmailHelper.DefaultSendMailTo(szSendTo, szCC, szBcc, szSubject, body);
            }
    
            private void button17_Click(object sender, EventArgs e)
            {
                var msg = "content";
                string errorMsg;
                EmailHelper.SendMessage("aisa@qq.com", "aisa@qq.com", "123456", "smtp.qq.com", 25,
                    new[] { "123456@qq.com" }, null, "测试Subject", msg, null, out errorMsg);
            }
  • 相关阅读:
    CSS选择器
    CSS框模型
    AJAX
    HTML 表单
    二叉树
    词嵌入、word2vec
    双向、深层循环神经网络
    20201012----每天都在进步吗?
    20201012--环境搭建
    20201011--记录一下
  • 原文地址:https://www.cnblogs.com/aisa-thanatos/p/10974107.html
Copyright © 2020-2023  润新知