Little and Mickle

All things are difficult before they are easy.

导航

Send email by jmail or CDO according to configuration


    项目做完在客户公司实施时,发现用jmail发不了邮件, 尝试用CDO, 发送成功.
    为了防止CDO发邮件不成功, 写了一个方法,根据配置文件来决定使用Jmail还是CDO来发送邮件.
    当Web.Config中的MailSender的key 值为1时,用CDO来发,为2时用Jmail来发.
    WebConfig配置(appSettings):
     <!-- Mail Sender 1 - CDO , 2 - Jmail -->
     <add key="MailSender" value="1" />

        #region SendEmailBase a method to send email for all SendMail() methods in SupplierBasePage
        
/// <summary>
        
/// a method to send email for all SendMail() methods
        
/// Send Email by JMail or CDO, determined by configuration
        
/// </summary>
        
/// <param name="emailTo"></param>
        
/// <param name="emailToName"></param>
        
/// <param name="content"></param>
        
/// <returns></returns>

        protected bool SendEmailBase(string emailTo,string emailToName,string content,string subject)
        
{
            
bool result = false;
            
try
            
{
                
if (Configuration.MailSender=="1")
                
{
                    result 
= SendEmailByCDO(emailTo,emailToName,content,subject);
                }

                
else
                
{
                    result 
= SendEmailByJmail(emailTo,emailToName,content,subject);
                }


                
            }

            
catch(Exception _ex)
            
{
                
throw _ex;
            }

            
            
return result;
        }

        
#endregion

一下是用jmail及CDO发email的代码:
        #region SendEmailByCDO
        
/// <summary>
        
/// Send Email By CDO
        
/// </summary>
        
/// <param name="emailTo"></param>
        
/// <param name="emailToName"></param>
        
/// <param name="content"></param>
        
/// <returns></returns>

        protected bool SendEmailByCDO(string emailTo,string emailToName,string content,string subject)
        
{        

            
bool Ret = false;
            
try
            
{
                
if (emailTo==null)                     
                
{
                    
return Ret;
                }


                CDO.Message oMsg 
= new CDO.MessageClass();
                oMsg.From 
= Configuration.From;
                oMsg.Subject 
= subject;
                oMsg.HTMLBody 
= content;
                
                oMsg.To  
= emailTo;


                CDO.IConfiguration iConfg 
= oMsg.Configuration;
                ADODB.Fields oFields 
= iConfg.Fields;

                oFields[
"http://schemas.microsoft.com/cdo/configuration/sendusing"].Value        = "2" ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = Configuration.From ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value  = Configuration.FromName ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/sendusername"].Value     = Configuration.UserAccount ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value     = Configuration.UserPassword ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = "1" ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/languagecode"].Value     = "0x0804" ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value       = Configuration.SMTPServer ;
                oFields[
"http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value   = Configuration.Port ;
                
                oFields.Update();
                oMsg.BodyPart.Charset
="utf-8";
                oMsg.HTMLBodyPart.Charset
="utf-8"

                oMsg.Send();
                oMsg 
= null;

                Ret 
= true;
            }

            
catch(Exception ex)
            
{
                
throw ex;
            }


            
return Ret;


        }

        
#endregion


        
#region SendEmailByJmail
        
/// <summary>
        
/// Send Email By Jmail
        
/// </summary>
        
/// <param name="emailTo"></param>
        
/// <param name="emailToName"></param>
        
/// <param name="content"></param>
        
/// <returns></returns>

        protected bool SendEmailByJmail(string emailTo,string emailToName,string content,string subject)
        
{
            
bool result = false;
            
try
            
{
                
                jmail.Message jmessage
=new jmail.MessageClass();

                jmessage.MailServerUserName
= Configuration.UserAccount;
                jmessage.MailServerPassWord
=  Configuration.UserPassword;

                jmessage.Logging 
= true;
                jmessage.Charset
="utf-8";
                jmessage.ContentType 
= "text/html";
                jmessage.Subject 
= subject;
                jmessage.From 
= Configuration.From;
                jmessage.FromName 
= Configuration.FromName;


                jmessage.AddRecipient(emailTo,emailToName,
"");

                
//jmessage.AddRecipient("futurefu@benq.com","Future","");

                
//jmessage.HTMLBody = MailString.MailBody(externaluser,MailTemplate.SUNotification);


                jmessage.HTMLBody 
= content;

                
bool success;

                
int tryTimes = 6;// 6 for default, if configured, use the configure value

                
try
                
{
                    tryTimes 
= Convert.ToInt32(Configuration.TryTimes);
                }

                
catch
                
{
                }

                
                
                
for(int i=0;i< tryTimes;i++)
                
{
                    success 
= jmessage.Send(Configuration.SMTPServer,false) ;
                    
if(success==true)
                    
{
                        result 
= true;
                        
break;
                    }

                    
else if(i == tryTimes-1 )
                    
{
                        result 
= false;
                        CommonFunction.MessageBox
                            (
"Failed to send notification email upon modification, Please contact your IFSZ PUR Responsible person! ");
                    }

                }
                

                jmessage.Close();

            }

            
catch(Exception _ex)
            
{
                
throw _ex;
            }


            
return result;

        }

        
#endregion

posted on 2005-08-24 18:12  davidullua  阅读(694)  评论(0)    收藏  举报