一个自己测试通过的发邮件操作

1。通用部分
None.gif using  System;
None.gif
using  System.Web.Mail;
None.gif
None.gif
None.gif
namespace  BusinessServer.DataServer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Mail 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class EMail
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public const  string C_strSenderName =  "******";
InBlock.gif        
public const  string C_strsendpassword =  "******";
InBlock.gif        
public const  string C_strSmtpServer =  "smtp.***.com";
InBlock.gif        
public const  string C_strFrom =  "******@***.com";
InBlock.gif        
public EMail()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static bool Send(string strTo, string strFrom, string strSubject, string strMessage)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MailMessage mailObj 
= new MailMessage(); 
InBlock.gif  
InBlock.gif                
// 设置email的'from'和'to'的地址 
InBlock.gif
                mailObj.From = C_strFrom; 
InBlock.gif                mailObj.To 
= strTo; 
InBlock.gif  
InBlock.gif                mailObj.Subject 
= strSubject; 
InBlock.gif                mailObj.Body 
= strMessage; 
InBlock.gif
InBlock.gif  
InBlock.gif                
// 可选: 使用html格式的Email 
InBlock.gif
                mailObj.BodyFormat = MailFormat.Html; 
InBlock.gif  
InBlock.gif                
// 可选: 对邮件进行加密 
InBlock.gif                
// mailObj.BodyEncoding = MailFormat.Base64; 
InBlock.gif  
InBlock.gif                
// 可选: 设置邮件的优先级别为高 
InBlock.gif
                mailObj.Priority = MailPriority.High; 
InBlock.gif   
InBlock.gif                mailObj.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1");    //basic authentication
InBlock.gif
                mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",C_strSenderName); //set your username here
InBlock.gif
                mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", C_strsendpassword);    //set your password here
InBlock.gif                
// 使用SmtpMail对象来发送邮件。 
InBlock.gif
                SmtpMail.SmtpServer = C_strSmtpServer;
InBlock.gif                SmtpMail.Send(mailObj);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ee)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Diagnostics.Debug.Write(ee.ToString());
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


2.调用部分
None.gif         private   bool  GetUserNameMenuName( ref   string  strUsers, ref   string  strMenuNames)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string strSQLUserName = string.Format("Select UserName FROM TBL_AccessUser Where RoleID = {0}  ",H_RoleID);
InBlock.gif            
string strSQLMenu = GetMenuSQL(H_AccessXml);
InBlock.gif            
string strSQL = string.Format("{0} ; {1} ",strSQLUserName,strSQLMenu);
InBlock.gif            DataSet  ds 
= DataProvider.RunSelect(strSQL);
InBlock.gif
InBlock.gif            
if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count == 0 )
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            
foreach(DataRow dr in ds.Tables[0].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(strUsers != "")
InBlock.gif                    strUsers 
+= ",";
InBlock.gif                strUsers 
+= dr["UserName"].ToString().Trim();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int icount = 1;
InBlock.gif            
foreach(DataRow dr in  ds.Tables[1].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strMenuName = dr["MenuName"].ToString().Trim();
InBlock.gif                
if (strMenuName != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (icount % 5 == 1)
InBlock.gif                        strMenuNames 
+= string.Format("<TR><TD>{0}</TD><TD>",strMenuName);  //
InBlock.gif
                    else if (icount % 5 == 0)
InBlock.gif                        strMenuNames 
+= string.Format("{0}</TD></TR>",strMenuName); //
InBlock.gif
                    else
InBlock.gif                        strMenuNames 
+= string.Format("{0}</TD><TD>",strMenuName);  //中间
InBlock.gif

InBlock.gif                    icount 
++;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return strUsers.Trim() != "";
ExpandedBlockEnd.gif        }

None.gif
None.gif
None.gif        
private   string  GetMessage()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            StringBuilder sbEmailMsg 
= new StringBuilder("");
InBlock.gif            
string strUser = "";
InBlock.gif            
string strMenuNames = "";
InBlock.gif            
InBlock.gif            
if (GetUserNameMenuName(ref strUser,ref strMenuNames))    
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sbEmailMsg.Append(
"<TABLE width='600' bgColor='#e0ecff'><caption>");
InBlock.gif                sbEmailMsg.Append(
"<caption style='FONT-SIZE: 20pt;COLOR: #f7f7f7; PADDING-TOP: 5px; BACKGROUND-COLOR: #4a3c8c'>权限角色修改记录</caption>");
InBlock.gif                sbEmailMsg.Append(
"<TR><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD><TD width='20%'></TD></TR>");
InBlock.gif                sbEmailMsg.Append(
string.Format("<TR><TD ColSpan=5>{0}的权限有变动,新权限为:</TD></TR>{1}</TABLE>",strUser,strMenuNames));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return sbEmailMsg.ToString();
ExpandedBlockEnd.gif        }

None.gif
None.gif
None.gif        
private   bool  DoSendMail()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string strSubject = "角色修改";
InBlock.gif            
string strMessage =    GetMessage();
InBlock.gif
InBlock.gif            
return EmailCommonFun.EmailSend(strSubject,strMessage);
ExpandedBlockEnd.gif        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值