C#.NET发送EMAIL的几种方法

转载 2012年11月21日 20:42:02

C#.NET发送EMAIL的常用几种方法 MailMessage/SmtpClient/CDO.Message

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net.Mail;
using System.Net;

namespace Pub.Class
{
    /// <summary>
    /// 发送Email类
    /// </summary>
    public class Email
    {
        #region 私有成员
        private static object lockHelper = new object();
        private string _From;
        private string _FromEmail;
        private string _Subject;
        private string _Body;
        private string _SmtpServer;
        private string _SmtpPort = "25";
        private string _SmtpUserName;
        private string _SmtpPassword;
        private System.Web.Mail.MailFormat _Format = System.Web.Mail.MailFormat.Html;
        private System.Text.Encoding _Encoding = System.Text.Encoding.Default;
        #endregion

        #region 属性
        /// <summary>
        /// 正文内容类型
        /// </summary>
        public System.Web.Mail.MailFormat Format { set { _Format = value; } }
        /// <summary>
        /// 正文内容编码
        /// </summary>
        public System.Text.Encoding Encoding { set { _Encoding = value; } }
        /// <summary>
        /// FromEmail 发送方地址(如test@163.com)
        /// </summary>
        public string FromEmail { set { _FromEmail = value; } }
        /// <summary>
        /// From
        /// </summary>
        public string From { set { _From = value; } }
        /// <summary>
        /// 主题
        /// </summary>
        public string Subject { set { _Subject = value; } }
        /// <summary>
        /// 内容
        /// </summary>
        public string Body { set { _Body = value; } }
        /// <summary>
        /// SmtpServer
        /// </summary>
        public string SmtpServer { set { _SmtpServer = value; } }
        /// <summary>
        /// SmtpPort
        /// </summary>
        public string SmtpPort { set { _SmtpPort = value; } }
        /// <summary>
        /// SmtpUserName
        /// </summary>
        public string SmtpUserName { set { _SmtpUserName = value; } }
        /// <summary>
        /// SmtpPassword
        /// </summary>
        public string SmtpPassword { set { _SmtpPassword = value; } }
        #endregion

        #region 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public Email() { }
        #endregion

        #region Send
        /// <summary>
        /// 发送EMAIL
        /// </summary>
        /// <example>
        /// <code>
        ///     Email _Email = new Email();
        ///     _Email.FromEmail = "test@163.com";
        ///     _Email.Subject = "&lt;div>aaaa&lt;/div>";
        ///     _Email.Body = "aaaaaaaaaaaaa";
        ///     _Email.SmtpServer = "smtp.163.com";
        ///     _Email.SmtpUserName = "aaa";
        ///     _Email.SmtpPassword = "aaa";
        ///     _Email.Send("test@163.com");
        /// </code>
        /// </example>
        /// <param name="toEmail">收信人 接收方地址</param>
        /// <returns>成功否</returns>
        public bool SmtpMailSend(string toEmail) {
            lock (lockHelper) {
                System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
                try {
                    msg.From = _FromEmail;//发送方地址(如test@163.com)
                    msg.To = toEmail;//接收方地址
                    msg.BodyFormat = _Format;//正文内容类型
                    msg.BodyEncoding = _Encoding;//正文内容编码
                    msg.Subject = _Subject;//主题
                    msg.Body = _Body;//内容
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//设置为需要用户验证
                    if (!_SmtpPort.Equals("25")) msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _SmtpPort);//设置端口
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _SmtpUserName);//设置验证用户名
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _SmtpPassword);//设置验证密码
                    System.Web.Mail.SmtpMail.SmtpServer = _SmtpServer;//邮件服务器地址(如smtp.163.com)
                    System.Web.Mail.SmtpMail.Send(msg);//发送
                    return true;
                } catch { } finally {
                   
                }
            }
            return false;
        }

        /// <summary>
        /// 发送EMAIL
        /// </summary>
        /// <param name="toEmail">Email</param>
        /// <returns>是否成功</returns>
        public bool CDOMessageSend(string toEmail) {
            lock (lockHelper) {
                CDO.Message bjMail = new CDO.Message();
                try {
                    objMail.To = toEmail;
                    objMail.From = _FromEmail;
                    objMail.Subject = _Subject;
                    if (_Format.Equals(System.Web.Mail.MailFormat.Html)) objMail.HTMLBody = _Body; else objMail.TextBody = _Body;
                    //if (!_SmtpPort.Equals("25")) objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value= _SmtpPort; //设置端口
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value= _SmtpServer;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value= 1;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"].Value= 10;
                    objMail.Configuration.Fields.Update();
                    objMail.Send();
                    return true;
                } catch {} finally{
                   
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
                bjMail = null;
            }
            return false;
        }
        /// <summary>
        /// CDOMessageSend
        /// </summary>
        /// <param name="toEmail"></param>
        /// <param name="sendusing"></param>
        /// <returns></returns>
        public bool CDOMessageSend(string toEmail,int sendusing) {
            lock (lockHelper) {
                CDO.Message bjMail = new CDO.Message();
                try {
                    objMail.To = toEmail;
                    objMail.From = _FromEmail;
                    objMail.Subject = _Subject;
                    if (_Format.Equals(System.Web.Mail.MailFormat.Html)) objMail.HTMLBody = _Body; else objMail.TextBody = _Body;
                    if (!_SmtpPort.Equals("25")) objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value= _SmtpPort; //设置端口
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value= _SmtpServer;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value= sendusing;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value= _FromEmail;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value= _FromEmail;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value= _SmtpUserName;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value= _SmtpUserName;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value= _SmtpPassword;
                    objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value=1;   

                    objMail.Configuration.Fields.Update();
                    objMail.Send();
                    return true;
                } catch { } finally{
                   
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail);
                bjMail = null;
            }
            return false;
        }
        /// <summary>
        /// SmtpClientSend
        /// </summary>
        /// <param name="toEmail"></param>
        /// <returns></returns>
        public bool SmtpClientSend(string toEmail) {
            lock (lockHelper) {
                System.Net.Mail.MailMessage message = new MailMessage(_FromEmail, toEmail, _Subject, _Body);
                message.SubjectEncoding = _Encoding;
                message.BodyEncoding = _Encoding;
                message.IsBodyHtml = true;
                message.Priority = MailPriority.High;

                SmtpClient client = new SmtpClient(_SmtpServer);
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(_SmtpUserName, _SmtpPassword);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Port = Str.ToInt(_SmtpPort, 587);
                client.EnableSsl = true;
                try {
                    client.Send(message);
                } catch {
                    return false;
                }
                return true;
            }
        }

        #endregion
    }
}


用.net发送 Email 帮助类 EmailHelper

上一篇转人家的, 虽然复制粘贴简单, 但找参数都要找半天的…… 发送邮件不就是知道人家的地址,再输入主题和内容就可以了嘛! 1. 配置文件 App.config ...
  • yenange
  • yenange
  • 2014年02月26日 15:31
  • 4730

C# QQ EMAIL 邮件批量 发送程序. 狂

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy...
  • luyikk
  • luyikk
  • 2007年03月06日 10:53
  • 2746

C#发送Email方法总结

通过.Net FrameWork 2.0下提供的“System.Net.Mail”可以轻松的实现,本文列举了3种途径来发送: 1.通过Localhost; 2.通过普通SMTP; 3....

使用C#发送邮件

最近有用户提出了一个新的需求,希望公司的ERP系统在交易申请书被批准以后自动发邮件到相关人员的邮箱中,让他们能第一时间知道。因为他们不想多打一次电话,也不愿意每天都开着ERP,但是他们的Outlook...
  • hb_gx
  • hb_gx
  • 2008年04月16日 23:46
  • 58786

C# MAIL CLASS

using System; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; using ...

C#调用Mail发送QQ邮件

需要用到: 1.System.Net.Mail; 2.QQ邮箱的POP3/SMTP服务码 QQ邮箱的POP3/SMTP服务码获取方法: 1.打开qq邮箱: 2.进入设置页面--》账户:(往下翻)...

Log4Net把日志保存到数据库和发送Email

1. 建立数据库的表和存储过程CREATE TABLE [dbo].[LogTest] ( [LogID] [int] IDENTITY (1, 1) NOT NULL , [LogDate] [...

C#一个完整的电子邮件操作类

/// /// 电子邮件操作类。 /// public class Mail { /// /// 初始化一个电子邮件操作类的空实例。 /// ...

C# SendEmail

Select-下拉列表选择插件(包含下拉选择图片,下拉grid等)

Select-下拉列表选择插件包含下拉选择图片,grid,下拉框选项的搜索,多个标签编辑,为下拉框中的每个选项添加不同图标等功能       1)ImageSelect是一个jQuery插件能够让用...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#.NET发送EMAIL的几种方法
举报原因:
原因补充:

(最多只允许输入30个字)