异步邮件发送

using System;
using System.Net.Mail;
using System.Collections.Specialized;

namespace MS.Core.Common
{
    
public sealed class SMTPEmailer
    {
        
#region 私有常量

        
private static string _fromAddress = "Jay.Sky001@163.com";
        
private static string _fromAlias = "Mr Vis";
        
private static string _toAlias = "Dear Friends";
        
private static string _toAddress = "MyMuse@foxmail.com";
        
private static string _ccAddress = "";//抄送
        private static string _bccAddress = "";//密送

        
private static string _smtpServer = "smtp.163.com";
        
private static bool _isEmailEnabled = true;

        
#endregion 

        
#region 私有变量

        
private static MailMessage message;

        
#endregion

        
#region 公有方法

        
static SMTPEmailer()
        {
        }

        
/// <summary>
        
/// 邮件发送重载方法
        
/// </summary>
        
/// <param name="subject"></param>
        
/// <param name="body"></param>
        public static void SendEmail(string subject, string body)
        {
            
if (_isEmailEnabled)
            {
                CreateMessage(subject, body);
                SendMail();
            }
        }

        
/// <summary>
        
/// 邮件发送重载方法
        
/// </summary>
        
/// <param name="subject"></param>
        
/// <param name="body"></param>
        
/// <param name="attachment"></param>
        public static void SendEmail(string subject, string body, string attachment)
        {
            
if (_isEmailEnabled)
            {
                CreateMessage(subject, body);

                
if (!string.IsNullOrEmpty(attachment))
                {
                    
if (System.IO.File.Exists(attachment))
                        message.Attachments.Add(
new Attachment(attachment));
                    
else
                        
throw new Exception(string.Format("Attachment {0} not found", attachment));
                }

                SendMail();
            }
        }


        
/// <summary>
        
/// 邮件发送重载方法
        
/// </summary>
        
/// <param name="fromAddress"></param>
        
/// <param name="fromAlias"></param>
        
/// <param name="toAddress"></param>
        
/// <param name="ccAddress"></param>
        
/// <param name="bccAddress"></param>
        
/// <param name="subject"></param>
        
/// <param name="body"></param>
        
/// <param name="attachment"></param>
        public static void SendEmail(string fromAddress, string fromAlias, string toAddress,
            
string ccAddress, string bccAddress, string subject, string body, string attachment)
        {
            
if (_isEmailEnabled)
            {
                _fromAddress 
= fromAddress;
                _fromAlias 
= fromAlias;
                _toAddress 
= toAddress;
                _ccAddress 
= ccAddress;
                _bccAddress 
= bccAddress;

                CreateMessage(subject, body);

                
if (!string.IsNullOrEmpty(attachment))
                {
                    
if (System.IO.File.Exists(attachment))
                        message.Attachments.Add(
new Attachment(attachment));
                    
else
                        
throw new Exception(string.Format("Attachment {0} not found", attachment));
                }

                SendMail();
            }
        }

        
#endregion

        
#region 私有方法

        
/// <summary>
        
/// 创建邮件信息
        
/// </summary>
        
/// <param name="subject"></param>
        
/// <param name="body"></param>
        private static void CreateMessage(string subject, string body)
        {
            
if (message == null)
            {
                message 
= new MailMessage();
                message.From 
= new MailAddress(_fromAddress, _fromAlias);
                message.Subject 
= subject;
                message.Body 
= body;
                
//message.To.Add(_toAddress);
                message.To.Add(new MailAddress(_toAddress, _toAlias));
                
if (!string.IsNullOrEmpty(_ccAddress))
                    message.CC.Add(_ccAddress);
                
if (!string.IsNullOrEmpty(_bccAddress))
                    message.Bcc.Add(_bccAddress);
            }
        }

        
/// <summary>
        
/// 发送邮件
        
/// </summary>
        private static void SendMail()
        {
            SmtpClient client 
= new SmtpClient(_smtpServer);
            client.DeliveryMethod 
= SmtpDeliveryMethod.Network;
            client.Credentials 
= new System.Net.NetworkCredential("邮箱账号""邮箱密码");
            client.SendCompleted 
+= new SendCompletedEventHandler(client_SendCompleted);
            client.SendAsync(message, 
"发送中..");
            message 
= null;
        }

        
/// <summary>
        
/// 邮件发送之后的异步操作
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            
// 获取此异步操作的独特标识符
            String token = (string)e.UserState;

            
if (e.Error != null)
            {
                
throw e.Error;
            }
            
else
            {
                
//成功后操作
            }
        }

        
#endregion 
    }
}
posted @ 2011-03-01 15:06  leeolevis  阅读(465)  评论(0编辑  收藏  举报