Eric's Blog

有需求才有进步

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Collections;

namespace GroupSendEmail
{
    /**//// <summary>
    /// Mail 的摘要说明
    /// </summary>
    public class CommonMail
    {
        private string _From;//发件人
        private string _To;//收件人
        private string _Subject;//邮件主题
        private string _Body;//邮件内容
        private string _Host;//邮件服务器
        private string _LoginName;//邮件服务器登录用户名
        private string _LoginPassword;//登录密码
        private int _Port;//邮件服务器端口
        private string _Msg;
        private bool _IsSuccess;
        public CommonMail()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 发件人
        /// </summary>
        public string From
        {
            get { return _From; }
            set { _From = value; }
        }
        /// <summary>
        /// 收件人
        /// </summary>
        public string To
        {
            get { return _To; }
            set { _To = value; }
        }
        /// <summary>
        /// 邮件主题
        /// </summary>
        public string Subject
        {
            get { return _Subject; }
            set { _Subject = value; }
        }
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Body
        {
            get { return _Body; }
            set { _Body = value; }
        }
        /// <summary>
        /// 邮件服务器
        /// </summary>
        public string Host
        {
            get { return _Host; }
            set { _Host = value; }
        }
        /// <summary>
        /// 邮件服务器登录用户名
        /// </summary>
        public string LoginName
        {
            get { return _LoginName; }
            set { _LoginName = value; }
        }
        /// <summary>
        /// 登录密码
        /// </summary>
        public string LoginPassword
        {
            get { return _LoginPassword; }
            set { _LoginPassword = value; }
        }
        /// <summary>
        /// 邮件服务器端口
        /// </summary>
        public int Port
        {
            get { return _Port; }
            set { _Port = value; }
        }

        public string Msg
        {
            get { return _Msg; }
            set { _Msg = value; }
        }

        public bool IsSuccess
        {
            get { return _IsSuccess; }
            set { _IsSuccess = value; }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool SendMail()
        {
            string Msg = "成功";

            try
            {
                SmtpClient scMain = new SmtpClient(_Host, _Port);
                scMain.EnableSsl = false;
                scMain.UseDefaultCredentials = true;
               
                if (_LoginName != "")
                {
                    scMain.Credentials = new System.Net.NetworkCredential(_LoginName, _LoginPassword);
                }
                scMain.DeliveryMethod = SmtpDeliveryMethod.Network;
                scMain.Send(_From, _To, _Subject, _Body);
                IsSuccess = true;
            }
            catch (Exception e)
            {
                Msg = e.ToString();
                IsSuccess = false;
            }

            return IsSuccess;
        }
    }
}

下面是在发邮件时要注意的事项:

1.注意公用邮箱的身份验证

很多公用邮箱为了反垃圾邮件,都加强了用户发邮件的限制,用户必须用自己的帐号和密码登录后才能发送邮件。所以一般要写上用户名和密码

2. 发送频率

不过频繁的发送,一般情况下半分钟发一个邮件是可以的,但如果过度频繁,做出垃圾邮件发送软件,会被邮件服务商给封号的,并且没有达到邮商限制的时间就发的话,很多邮件会发不出去。

3. 注意放火墙和杀毒软件

 

posted on 2008-03-31 15:56  Eric.Chai  阅读(525)  评论(0编辑  收藏  举报