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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

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