• c# SendMail


    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Mail;
    using System.Text;
    
    namespace Common
    {
        /// <summary>
        /// 基于system.net.mail发送邮件,支持附件
        /// </summary>
        public class NetSendMail
        {
            public static void MailSend(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent, IList<string> mailAttachments, System.Text.Encoding encoding, bool isBodyHtml)
            {
                MailMessage message = new MailMessage();
                if (mailFrom.Trim() == "")
                {
                    throw new Exception("发送邮件不可以为空");
                }
                message.From = new MailAddress(mailFrom);
                if (mailTo.Count <= 0)
                {
                    throw new Exception("接收邮件不可以为空");
                }
                foreach (string s in mailTo)
                {
                    message.To.Add(new MailAddress(s));
                }
                if (mailCC.Count > 0)
                {
                    foreach (string s in mailCC)
                    {
                        message.CC.Add(new MailAddress(s));
                    }
                }
                if (mailBCC.Count > 0)
                {
                    foreach (string s in mailBCC)
                    {
                        message.Bcc.Add(new MailAddress(s));
                    }
                }
                message.Subject = mailTitle;
                message.Body = mailContent;
                message.BodyEncoding = encoding;   //邮件编码
                message.IsBodyHtml = isBodyHtml;      //内容格式是否是html
                message.Priority = MailPriority.High;  //设置发送的优先集
                //附件
                foreach (string att in mailAttachments)
                {
                    message.Attachments.Add(new Attachment(att));
                }
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = mailSmtpServer;
                smtpClient.Credentials = new NetworkCredential(maiFromlAccount, mailFromPwd);
                smtpClient.Timeout = 1000;
                smtpClient.EnableSsl = false;        //不使用ssl连接
                smtpClient.Send(message);
            }
    
            public static void MailSendText(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
            {
                List<string> attList = new List<string>();
                MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, false);
            }
    
            public static void MailSendHTML(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList<string> mailTo, IList<string> mailCC, IList<string> mailBCC, string mailTitle, string mailContent)
            {
                List<string> attList = new List<string>();
                MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, true);
            }
        }
    }
  • 相关阅读:
    人工智能实战2019第一次作业_李大
    300道四则运算题目
    build to win读后感
    C# WinForm(简易计算器示例)
    WPF关于Calendar的Style
    winform下实现Label在窗体中从右到左不断循环的动画效果
    WPF 选中datagrid列头中checkbox则全选对应整列checkbox
    Window下android 模拟器SD卡的使用方法
    Linux下在Android模拟器中使用SD 卡的操作步骤
    WPF之VLC流媒体播放
  • 原文地址:https://www.cnblogs.com/xsmhero/p/3300828.html
Copyright © 2020-2023  润新知