using System;
using System.Collections.Generic;
using System.Text;
using Common;//email用到的命名空间using System.Collections;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Diagnostics;namespace SendMail
{
    class SendMail
    {
        public SendMail()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }        public void DoSent(string AddressName, string email_s,string email_s_pass,  StringBuilder sbLog, StringBuilder sbLogError, string total, string docu, string filepath)
        {
            string email_r = "";
            email_r = conn.ExecSqlNonQuery("SELECT TOP 1 EMAIL FROM SYSUSER WHERE NAME = '" + AddressName + "'");
            send(AddressName, email_r, email_s, email_s_pass, sbLogError, sbLog, total, docu, filepath);
        }        private void send(string AddressName, string email_r, string email_s, string email_s_pass, StringBuilder sbLogError, StringBuilder sbLog, string total, string docu, string filepath)
        {
            string smtp_service = "****.*****.com"; //需要根据不同公司设定。发件人的smtp值。            if (IsEmail(email_r) == false)
            {
                sbLogError.Append("收件人" + AddressName + "邮件格式不对!");
                return;
            }            if (IsEmail(email_s) == false)
            {
                sbLogError.Append("发件人邮件格式不对!");
                return;
            }
            
            CreateMessageWithAttachment(smtp_service, AddressName, email_r, email_s, email_s_pass, total, docu, filepath, sbLogError, sbLog);        }
        //发邮件
        private void CreateMessageWithAttachment(string server, string AddressName, string email_r, string email_s, string email_s_pass,string total, string docu, string filepath, StringBuilder sbLogError, StringBuilder sbLog)
        {
            // Specify the file to be attached and sent.
            // This example assumes that a file named Data.xls exists in the
            // current working directory.
            Attachment data = new Attachment(filepath, MediaTypeNames.Application.Octet);
            try
            {
                // Create a message and set up the recipients.
                MailMessage message = new MailMessage(
                   email_s,//发件人
                   email_r,//收件人
                   total,//主题 
                   docu //内容
                   );                // Create  the file attachment for this e-mail message.
                message.Attachments.Add(data);
                
                SmtpClient client = new SmtpClient(server);
                // Add credentials if the SMTP server requires them.         
                client.Credentials = new System.Net.NetworkCredential(email_s, email_s_pass);//用户名和密码
                client.Send(message);
                sbLog.Append("发送邮件至" + AddressName + "成功" + "\r\n");
            }
            catch 
            {
                sbLogError.Append("发送邮件至" + AddressName + "失败" + "\r\n");
            }
            finally
            {
                data.Dispose();
            }
        }
        //判断输出邮件地址格式
        private static bool IsEmail(string Str)
        {
            return Regex.IsMatch(Str, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
        }
    }
}
上面是我写的一段代码,发送email的,同事都很忙没空帮我看,我也写这个快一年了,没人指点我代码的规范,我自己总觉得自己写的代码没人家写的美,自己也不知道如何改,请大伙帮我看看,分数就剩这么多了,不好意思,

解决方案 »

  1.   

    //code1
    public string GetStr(string inputStr,string reutrnstr)
    {
    }
    //code2
    public string getStr(string inputStr,string reutrnStr)
    {
    }
      

  2.   


    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using Common; //email用到的命名空间 using System.Collections; 
    using System.Net.Mail; 
    using System.Net; 
    using System.Net.Mime; 
    using System.Text.RegularExpressions; 
    using System.Diagnostics; 
    namespace SendMail 

        class SendMail 
        { 
            public SendMail() 
            { 
                // 
                // TODO: 在此处添加构造函数逻辑 
                // 
            }         public void DoSent(string AddressName, string email_s,string email_s_pass,  StringBuilder sbLog, StringBuilder sbLogError, string total, string docu, string filepath) 
            { 
                string email_r = ""; 
                email_r = conn.ExecSqlNonQuery("SELECT TOP 1 EMAIL FROM SYSUSER WHERE NAME = '" + AddressName + "'"); 
                send(AddressName, email_r, email_s, email_s_pass, sbLogError, sbLog, total, docu, filepath); 
            } 
            private void send(string AddressName, string email_r, string email_s, string email_s_pass, StringBuilder sbLogError, StringBuilder sbLog, string total, string docu, string filepath) 
            { 
                string smtp_service = "****.*****.com"; //需要根据不同公司设定。发件人的smtp值。             if (IsEmail(email_r) == false) 
                { 
                    sbLogError.Append("收件人" + AddressName + "邮件格式不对!"); 
                    return; 
                }             if (IsEmail(email_s) == false) 
                { 
                    sbLogError.Append("发件人邮件格式不对!"); 
                    return; 
                } 
                
                CreateMessageWithAttachment(smtp_service, AddressName, email_r, email_s, email_s_pass, total, docu, filepath, sbLogError, sbLog);         } 
            //发邮件 
            private void CreateMessageWithAttachment(string server, string AddressName, string email_r, string email_s, string email_s_pass,string total, string docu, string filepath, StringBuilder sbLogError, StringBuilder sbLog) 
            { 
                // Specify the file to be attached and sent. 
                // This example assumes that a file named Data.xls exists in the 
                // current working directory. 
                Attachment data = new Attachment(filepath, MediaTypeNames.Application.Octet); 
                try 
                { 
                    // Create a message and set up the recipients. 
                    MailMessage message = new MailMessage( 
                      email_s,//发件人 
                      email_r,//收件人 
                      total,//主题 
                      docu //内容 
                      );                 // Create  the file attachment for this e-mail message. 
                    message.Attachments.Add(data); 
                    
                    SmtpClient client = new SmtpClient(server); 
                    // Add credentials if the SMTP server requires them.        
                    client.Credentials = new System.Net.NetworkCredential(email_s, email_s_pass);//用户名和密码 
                    client.Send(message); 
                    sbLog.Append("发送邮件至" + AddressName + "成功" + "\r\n"); 
                } 
                catch 
                { 
                    sbLogError.Append("发送邮件至" + AddressName + "失败" + "\r\n"); 
                } 
                finally 
                { 
                    data.Dispose(); 
                } 
            } 
            //判断输出邮件地址格式 
            private static bool IsEmail(string Str) 
            { 
                return Regex.IsMatch(Str, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); 
            } 
        } 

      

  3.   

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using Common; //email用到的命名空间 using System.Collections; 
    using System.Net.Mail; 
    using System.Net; 
    using System.Net.Mime; 
    using System.Text.RegularExpressions; 
    using System.Diagnostics; 
    namespace SendMail 

        class SendMail 
        { 
            public SendMail() 
            { 
                // 
                // TODO: 在此处添加构造函数逻辑 
                // 
            }         public void DoSent(string AddressName, string email_s,string email_s_pass,  StringBuilder sbLog, StringBuilder sbLogError, string total, string docu, string filepath) 
            { 
                string email_r = ""; 
                email_r = conn.ExecSqlNonQuery("SELECT TOP 1 EMAIL FROM SYSUSER WHERE NAME = '" + AddressName + "'"); 
                send(AddressName, email_r, email_s, email_s_pass, sbLogError, sbLog, total, docu, filepath); 
            } 
            private void send(string AddressName, string email_r, string email_s, string email_s_pass, StringBuilder sbLogError, StringBuilder sbLog, string total, string docu, string filepath) 
            { 
                string smtp_service = "****.*****.com"; //需要根据不同公司设定。发件人的smtp值。             if (IsEmail(email_r) == false) 
                { 
                    sbLogError.Append("收件人" + AddressName + "邮件格式不对!"); 
                    return; 
                }             if (IsEmail(email_s) == false) 
                { 
                    sbLogError.Append("发件人邮件格式不对!"); 
                    return; 
                } 
                
                CreateMessageWithAttachment(smtp_service, AddressName, email_r, email_s, email_s_pass, total, docu, filepath, sbLogError, sbLog);         } 
            //发邮件 
            private void CreateMessageWithAttachment(string server, string AddressName, string email_r, string email_s, string email_s_pass,string total, string docu, string filepath, StringBuilder sbLogError, StringBuilder sbLog) 
            { 
                // Specify the file to be attached and sent. 
                // This example assumes that a file named Data.xls exists in the 
                // current working directory. 
                Attachment data = new Attachment(filepath, MediaTypeNames.Application.Octet); 
                try 
                { 
                    // Create a message and set up the recipients. 
                    MailMessage message = new MailMessage( 
                      email_s,//发件人 
                      email_r,//收件人 
                      total,//主题 
                      docu //内容 
                      );                 // Create  the file attachment for this e-mail message. 
                    message.Attachments.Add(data); 
                    
                    SmtpClient client = new SmtpClient(server); 
                    // Add credentials if the SMTP server requires them.        
                    client.Credentials = new System.Net.NetworkCredential(email_s, email_s_pass);//用户名和密码 
                    client.Send(message); 
                    sbLog.Append("发送邮件至" + AddressName + "成功" + "\r\n"); 
                } 
                catch 
                { 
                    sbLogError.Append("发送邮件至" + AddressName + "失败" + "\r\n"); 
                } 
                finally 
                { 
                    data.Dispose(); 
                } 
            } 
            //判断输出邮件地址格式 
            private static bool IsEmail(string Str) 
            { 
                return Regex.IsMatch(Str, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); 
            } 
        } 

    这样发出来,看的清晰点
      

  4.   

    1.参考MSDN的《.net类库设计准则》
    2.使用Microsoft FXCop工具或CodStyle(可能拼写错误)工具,前者针对项目,对dll,exe进行检则,后者是vs的一个插件,可随时对项目,类进行检则
      

  5.   

    private void CreateMessageWithAttachment(string server, string AddressName, string email_r, string email_s, string email_s_pass,string total, string docu, string filepath, StringBuilder sbLogError, StringBuilder sbLog)
    若是没弄错的话 
    使用了PASSCAL命名法,还有匈牙利命名法,还有linux下面的习惯。LZ博采众长呀