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

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - 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