C#发送邮件主要代码

代码

代码

代码
--------------------------


using System;using System.Web;using System.Net;using System.Net.Mail;using System.Xml;using System.Configuration;using System.IO;using System.Text;using System.Collections.Generic;namespace TSF.Ent.Common.Utils{ /// <summary> /// 邮件发送工具 /// </summary> public class MailUtil { private MailUtil() { } /// <summary> /// 发送邮件 /// </summary> /// <param name="receiver">邮件接受者</param> /// <param name="subject">邮件标题</param> /// <param name="body">邮件正文</param> /// <param name="attachments">附件</param> public static void Send(string receiver, string subject, string body, IList<Attachment> attachments) { Send(SmtpConfig.Provider.SmtpSetting.Sender, receiver, subject, body, attachments); } /// <summary> /// 发送邮件 /// </summary> /// <param name="sender">邮件发送者</param> /// <param name="receiver">邮件接受者</param> /// <param name="subject">邮件标题</param> /// <param name="body">邮件正文</param> /// <param name="attachments">附件</param> public static void Send(string sender, string receiver, string subject, string body, IList<Attachment> attachments) { Send(SmtpConfig.Provider.SmtpSetting.Host, SmtpConfig.Provider.SmtpSetting.Port, SmtpConfig.Provider.SmtpSetting.UserName, SmtpConfig.Provider.SmtpSetting.Password, sender, receiver, subject, body, attachments); } /// <summary> /// 发送邮件 /// </summary> /// <param name="host">邮件服务器</param> /// <param name="port">端口</param> /// <param name="userName">邮件服务器登录用户</param> /// <param name="passwrod">邮件服务器登录密码</param> /// <param name="sender">邮件发送者</param> /// <param name="receiver">邮件接受者</param> /// <param name="subject">邮件标题</param> /// <param name="body">邮件正文</param> /// <param name="attachments">附件</param> public static void Send(string host, int port, string userName, string passwrod, string sender, string receiver, string subject, string body, IList<Attachment> attachments) { SmtpClient smtpClient = new SmtpClient(host, port); MailMessage msg = new MailMessage(sender, receiver); msg.IsBodyHtml = true; msg.SubjectEncoding = Encoding.GetEncoding("utf-8"); msg.BodyEncoding = Encoding.GetEncoding("utf-8"); msg.Subject = subject; msg.Body = body; msg.Attachments.Clear(); if (attachments != null) { foreach (Attachment attach in attachments) { // 增加附件 msg.Attachments.Add(attach); } } smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(userName, passwrod); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Send(msg); } } /// <summary> /// 配置邮件服务器 /// </summary> internal class SmtpConfig { private static SmtpConfig _smtpConfig; private SmtpSetting _smtpSetting; private SmtpConfig() { } /// <summary> /// 获取邮件服务器配置 /// </summary> public static SmtpConfig Provider { get { if (_smtpConfig == null) { _smtpConfig = new SmtpConfig(); } return _smtpConfig; } } /// <summary> /// 配置信息 /// </summary> public SmtpSetting SmtpSetting { get { if (_smtpSetting == null) { XmlDocument doc = new XmlDocument(); doc.Load(this.ConfigFile); _smtpSetting = new SmtpSetting() { Host = doc.DocumentElement.SelectSingleNode("Host").InnerText, Port = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("Port").InnerText), UserName = doc.DocumentElement.SelectSingleNode("UserName").InnerText, Password = doc.DocumentElement.SelectSingleNode("Password").InnerText, Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText }; } return _smtpSetting; } } /// <summary> /// 读取配置文件 /// </summary> private string ConfigFile { get { string configPath = ConfigurationManager.AppSettings["SmtpConfig"]; if (HttpContext.Current != null) { configPath = HttpContext.Current.Server.MapPath(configPath); } else // 非Web { configPath = configPath.Replace("/", "\\"); configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configPath); } return configPath; } } } internal class SmtpSetting { private string _host; private int _port = 25; private string _userName; private string _password; private string _sender; public string Host { get { return _host; } set { _host = value; } } public int Port { get { return _port; } set { _port = value; } } public string UserName { get { return _userName; } set { _userName = value; } } public string Password { get { return _password; } set { _password = value; } } public string Sender { get { return _sender; } set { _sender = value; } } }}
邮件配置
---------------------------


<!-- 在Web.config或者App.config增加 --><!-- ------------------------------------------ --><!-- <appSettings> --><!-- <add key="SmtpConfig" value="Config/SmtpSetting.config"/> --><!-- </appSettings> --><!-- ------------------------------------------ --><!-- SmtpSetting.config --> <!-- ------------------------------------------ --><SmtpSetting> <Host>pop3.server.com</Host> <Port>25</Port> <UserName>yourcompany@site.com</UserName> <Password>yourpassword</Password> <Sender>yourcompany@site.com</Sender></SmtpSetting>
发送测试
---------------------------


static void Main(string[] args){IList<Attachment> attachs = new List<Attachment>();Attachment attach = new Attachment(@"E:\docs\慢慢地才知道.txt");attachs.Add(attach);for (int i = 0; i < 10; i++){// 测试MailUtil.Send(" yourname@163.com , "测试邮件", "测试邮件正文", attachs);}System.Console.ReadKey();}
来源:https://www.cnblogs.com/AaronWu/archive/2010/12/05/1896779.html