惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
G
Google Developers Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
博客园 - 聂微东
V
Visual Studio Blog
博客园_首页
D
DataBreaches.Net
腾讯CDC
I
InfoQ
F
Fortinet All Blogs
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
月光博客
月光博客
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog

博客园 - davidullua

Google chrome的离线安装版 百度进军B2C行业 File Comparer - To compare two files and check whether they have the same content No size set for variable length data type: String Response.WriteFile()下载文件,打开对话框出现两次 - davidullua - 博客园 to create table if table do not exist -- to backup and restore table in oracle Gmail邀请 to call Session_End() event when user closed Browser When Session_End() and Session_Start() Fires about Ora-03114 disable asp.net connection pool(pooling) Oracle FAQ: 未在本地计算机上注册"OraOLEDB.Oracle"提供程序 (The 'OraOLEDB.Oracle' provider is not registered on the local machine) solution - davidullua ora-00054 , alter system kill session 'id,serial#' Change Path of C:\Documents and Settings - davidullua About FormsAuthentication.RedirectFromLoginPage(string username, bool createPersistentCookie) C#中调用控件的事件 [导入]C#向Sql Server中插入记录时单引号的处理 [导入]C#使用CDO发送邮件 [导入]使用jmail组件发送电子邮件(C#) [导入]C#中使用反射显示程序集的所有类型和属性 [导入]使用Oracle Developer Tools For Visual Studio .NET
Send email by jmail or CDO according to configuration
davidullua · 2005-08-24 · via 博客园 - davidullua

    项目做完在客户公司实施时,发现用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