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. Java IO总结之缓冲读入文件

    package com.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException ...

  2. 夺命雷公狗---linux之centos的安装

    由于要玩node.js了,所以还是来复习下linux系统才行,所以夺命雷公狗分享两套安装linux的方法,这是centos的安装方法,,, 管理员默认帐号为:root,密码则是刚才您输入的那个...

  3. PAT乙级 1011. A+B和C (15)

    1011. A+B和C (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 给定区间[-231, 231 ...

  4. getResource().getPath()返回的路径空格变成了 %20

    this.getClass().getResource(“/”).getPath()使用者方法查看文件在服务器上的地址,但是地址中的空格会被转化为%20. 解决办法1: URI uri = new U ...

  5. archlinux 网络配置

    https://wiki.archlinux.org/index.php/Network_configuration_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%2 ...

  6. z/os上的tar和gzip

    在*nix平台上玩过的人都知道,tar和gzip基本上是每天都要使用的,而且非常之好用.而Mainframer则比较痛苦,没有这么好用的东西,尤其是当需要通过网络传大批量的文件的时候很不方便. 不过总 ...

  7. 《Focus On 3D Terrain Programming》中一段代码的注释三

    取自<Focus On 3D Terrain Programming>中的一段: //--------------------------------------------------- ...

  8. 5 Best Automation Tools for Testing Android Applications

    Posted In | Automation Testing, Mobile Testing, Software Testing Tools   Nowadays automated tests ar ...

  9. Ubuntu 下配置ftp服务端

    安装sudo apt-get install pure-ftpdsudo netstat –ano | grep “21”查看21端口是否已开启.完成安装后,就可以用Ubuntu系统的用户名和密码登陆 ...

  10. Memcached 分布式缓存实现原理

    摘要 在高并发环境下,大量的读.写请求涌向数据库,此时磁盘IO将成为瓶颈,从而导致过高的响应延迟,因此缓存应运而生.无论是单机缓存还是分布式缓存都有其适应场景和优缺点,当今存在的缓存产品也是数不胜数, ...