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 <= )
{
throw new Exception("接收邮件不可以为空");
}
foreach (string s in mailTo)
{
message.To.Add(new MailAddress(s));
}
if (mailCC.Count > )
{
foreach (string s in mailCC)
{
message.CC.Add(new MailAddress(s));
}
}
if (mailBCC.Count > )
{
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 = ;
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);
}
}
}

c# SendMail的更多相关文章

  1. centos 6.6 系统中配置sendmail和dovecot

    网上介绍sendmail的文章千百种,很少有跟着做下来一次成功的.多少都有些说的不准确的地方. 我给大家共享一下我经过实验环境测试,完全可行的方法. 1.软件准备 操作系统:centos6.6我选择c ...

  2. 如何利用sendmail发送外部邮件?

    在写监控脚本时,为了更好的监控服务器性能,如磁盘空间.系统负载等,有必要在系统出现瓶颈时,及时向管理员进行报告.在这里通常采用邮件报警,同时,邮件设置为收到邮件,即向指定手机号码发送短信.这样可以实现 ...

  3. Linux 通过sendmail 发邮件到外部邮箱

    最近在写自动化巡检脚本,想着怎么预警后自动发送邮件报警. 首先下载最新版本mailx-12.4.tar.bz2 # wget http://sourceforge.net/projects/heirl ...

  4. Red Hat Enterprise Server 6.0 安装Sendmail相关包

    由于需要在Linux服务器(Red Hat Enterprise Linux Server release 6.0)上配置邮件服务,需要安装Sendmail包,一般Sendmail的安装有两种方式:R ...

  5. Linux sendmail发送邮件失败诊断案例(一)

    在新服务器上测试sendmail发送邮件时,发现邮件发送不成功,检查日志文件发现如下错误(Notice:由于涉及公司服务器,邮箱等,故下面hostname.邮箱地址等信息使用xxx代替) tail - ...

  6. Linux SendMail发送邮件失败诊断案例(二)

    Linux上Sendmail经常由于一些配置问题,导致邮件发送失败,下面整理.收集了一些邮件发送失败.异常的案例. 案例1:在新服务器上测试sendmail发送邮件时,发现邮件发送不成功,检查/var ...

  7. postfix与sendmail冲突

    在Linux服务器(CentOS release 6.6)上配置好了sendmail后,测试发送邮件时发现有问题,检查sendmail服务的状态,发现其处于"sendmail dead bu ...

  8. Linux SendMail服务启动慢总结

    在 CentOS release 6.6 上启动sendmail服务时发现服务启动过程非常慢,基本上要耗费3分多钟.有点纳闷:什么原因导致sendmail启动这么慢?搜索了这方面的一些资料,结合自己的 ...

  9. linux系统下sendmail的搭建

    学习鸟哥linux私房菜所得 sendmail 可以使用rpm -qa |grep sendmail来查看一下是否已安装sendmail-cf和sendmail 如果没有安装可用yum -y inst ...

  10. PHP (sendmail / PHPMailer / ezcMailComposer)发送邮件

    一. 使用 PHP 内置的 mail() 函数 1. Windows 下 环境:WampServer2.5(Windows 10,Apache 2.4.9,MySQL 5.6.17,PHP 5.5.1 ...

随机推荐

  1. PAT乙级 1022. D进制的A+B (20)

    1022. D进制的A+B (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 输入两个非负10进制整数A和 ...

  2. MYSQL日期类型的加减更新使用INTERVAL 1 DAY

    例如:UPDATE teachingplan SET teachPlanBeginTime = teachPlanBeginTime +INTERVAL 1 DAY

  3. OpenGl And 视图

    OpenGl And 视图 标签(空格分隔): game 简介 本文主要介绍坐标系的观念, 以及在openGL中的视图及其相关的变换. 大纲 视图.模型.投影变换概念 Opengl中对各种变换的支持 ...

  4. win7 IIS发布项目遇到的问题

     问题一: HTTP 错误 404.3 – Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 1.依次打开控制面板 ...

  5. form 表单jquery验证插件使用

    第一部分:表单样式 <form action="#" method="post" id="regist">   <tabl ...

  6. Hadoop实战2:MapReduce编程-WordCount实例-streaming-python环境

    这是搭建hadoop环境后的第一个MapReduce程序: 基于hadoop streaming的python的脚本: 1 map.py文件,把文本的内容划分成单词: #!/usr/bin/pytho ...

  7. JS货币数字转换中文

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Android NDK 开发(一)--环境搭建【转】

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/41800955 Android下的NDK开发是Android开发中不可或缺的一部分, ...

  9. docker RESTful API

    https://docs.docker.com/engine/reference/api/docker_remote_api/

  10. 设置eclipse补全方法

    开MyEclipse 6.0. 1,然后“window”→“Preferences” 2,选择“java”,展开,“Editor”,选择“Content Assist”. 3,选择“Content A ...