博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# SendMail
阅读量:5939 次
发布时间:2019-06-19

本文共 2989 字,大约阅读时间需要 9 分钟。

using System;using System.Collections.Generic;using System.Net;using System.Net.Mail;using System.Text;namespace Common{    ///     /// 基于system.net.mail发送邮件,支持附件    ///     public class NetSendMail    {        public static void MailSend(string mailFrom, string maiFromlAccount, string mailFromPwd, string mailSmtpServer, IList
mailTo, IList
mailCC, IList
mailBCC, string mailTitle, string mailContent, IList
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
mailTo, IList
mailCC, IList
mailBCC, string mailTitle, string mailContent) { List
attList = new List
(); 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
mailTo, IList
mailCC, IList
mailBCC, string mailTitle, string mailContent) { List
attList = new List
(); MailSend(mailFrom, maiFromlAccount, mailFromPwd, mailSmtpServer, mailTo, mailCC, mailBCC, mailTitle, mailContent, attList, Encoding.UTF8, true); } }}

 

转载于:https://www.cnblogs.com/xsmhero/p/3300828.html

你可能感兴趣的文章
关于缓存命中率的几个关键问题!
查看>>
java的日历类
查看>>
mybatis 思维导图,让 mybatis 不再难懂(二)
查看>>
搭建百度unit2.0测试代码(Java)
查看>>
DOS常用命令详解
查看>>
监控haproxy的脚本
查看>>
单用户模式迁移home家目录
查看>>
Css常用操作——————分类
查看>>
Wrong permissions on configuration file, should not be world writable
查看>>
rabbitmq3.5.1 原理和集群安装
查看>>
linux中的rm 删除命令
查看>>
多功能PCIE交换机之三:PCIE非透明桥 cache一致性
查看>>
依赖属性之“风云再起”三
查看>>
9.1 正则介绍_grep(上);9.2 grep(中);9.3 grep(下)
查看>>
启动oracle em命令
查看>>
jquery option 动态 selected
查看>>
搭建简易堡垒机
查看>>
鸟哥的linux私房菜-文件压缩于打包-2
查看>>
coreutils-5.0中几个命令的执行过程
查看>>
F5 bigip.conf配置问题
查看>>