阿牛 - 专注.NET开发

如果梦想与实现之间有一道不可逾越的鸿沟,那么“执行力”就是跨越这道鸿沟的桥梁。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

方法一

System.Web.Mail名字空间下的SmtpMail.Send(MailMessage mail)静态方法
这种方法最为简单,是.NET1.1中常用的方法,一般的SMTP服务都能支持。

方法二
System.Net.Mail名字空间下的SmtpClient.Send(MailMesasge mail)实例方法

这是.NET2.0里推荐的方法,它更加灵活,但写起来复杂一点。像Gmail这样需要SSL安全连接认证的,方法一是不行的。


两种方法代码示例:

 

/// <summary>
    
/// Send e-mail hepler class
    
/// </summary>

    public sealed class EmailHelper
    
{
        
public static void SendWebMail(IList<string> toList, IList<string> ccList, 
            
string subject, string body)
        
{
            System.Web.Mail.MailMessage mail 
= new System.Web.Mail.MailMessage();
            SmtpMail.SmtpServer 
= SystemConfig.SmtpServer;

            
if (toList==null || toList.Count ==0)
            
{
                
throw new ArgumentNullException("toList can be null or empty.");
            }


            StringBuilder sb 
= new StringBuilder();

            
foreach (string s in toList)
            
{
                sb.Append(s);
                sb.Append(
";");
            }

            mail.To 
= sb.ToString();

            sb.Remove(
0, sb.Length);
            
if (ccList != null && ccList.Count != 0)
            
{
                
foreach (string s in ccList)
                
{
                    sb.Append(s);
                    sb.Append(
";");
                }

            }

            mail.Cc 
= sb.ToString();
            
            
//基本权限 
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
                
"1");
            
//用户名 
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername"
                SystemConfig.SenderUserId);
            
//密码 
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                SystemConfig.SenderPassword);

            mail.From 
= SystemConfig.SenderEmail;
            mail.Subject 
= subject;
            mail.BodyFormat 
= MailFormat.Html;
            mail.Body 
= body;
            
            SmtpMail.Send(mail);           
        }


        
public static void SendNetMail(IList<string> toList, IList<string> ccList,
            
string subject, string body)
        
{
            System.Net.Mail.MailMessage mail 
= new System.Net.Mail.MailMessage();            

            
if (toList == null || toList.Count == 0)
            
{
                
throw new ArgumentNullException("toList can be null or empty.");
            }
        

            
foreach (string s in toList)
            
{
                mail.To.Add(s);
            }


            
if (ccList!= null && ccList.Count != 0)
            
{
                
foreach (string s in ccList)
                
{
                    mail.CC.Add(s);
                }

            }


            mail.From 
= new System.Net.Mail.MailAddress(SystemConfig.SenderEmail, SystemConfig.SenderUserId,Encoding.UTF8);
            mail.Subject 
= subject;
            mail.IsBodyHtml 
= true;
            mail.Body 
= body;

            SmtpClient client 
= new SmtpClient(SystemConfig.SmtpServer, SystemConfig.SmtpPort);
            client.Credentials 
= new System.Net.NetworkCredential(SystemConfig.SenderUserId, SystemConfig.SenderPassword);
            client.EnableSsl 
= SystemConfig.EnableSSL;
            client.Send(mail);
        }

    }

 

// <summary>
    /// Stores system config / paramerter / hardcoded values
    
/// </summary>

    public sealed class SystemConfig
    
{       

        
public static string SmtpServer
        
{
            
get
            
{
                
return ConfigurationManager.AppSettings["smtpServer"];
            }

        }


        
public static int SmtpPort
        
{
            
get
            
{
                
return int.Parse(ConfigurationManager.AppSettings["smtpPort"]);
            }

        }


        
public static string SenderEmail
        
{
            
get
            
{
                
return ConfigurationManager.AppSettings["senderEmail"];
            }

        }


        
public static string SenderUserId
        
{
            
get
            
{
                
return ConfigurationManager.AppSettings["senderUserId"];
            }

        }


        
public static string SenderPassword
        
{
            
get
            
{
                
return ConfigurationManager.AppSettings["senderPassword"];
            }

        }


        
public static bool EnableSSL
        
{
            
get
            
{
                
return bool.Parse(ConfigurationManager.AppSettings["EnableSSL"]);
            }

        }

    }


web.config

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<connectionStrings>
        
<add name="pbcrm" connectionString="Server=192.168.2.58;Initial Catalog=P;User ID=sa;PassWord=" providerName="System.Data.SqlClient"/>
    
</connectionStrings>

    
<appSettings>
        
<add key="smtpServer" value="smtp.gmail.com"/>
        
<add key="smtpPort" value="587"/>
        
<add key="senderEmail"  value="rockniu@gmail.com"/>
        
<add key="senderUserId" value="rockniu"/>
        
<add key="senderPassword" value="******"/>
        
<add key ="EnableSSL" value="true"/>
     
</appSettings>    
</configuration>

 


测试代码:

 


    [TestFixture]
    
public class SendMailTest 
    
{
        List
<string> _sendTo = new List<string>(10);
        List
<string> _copyTo = new List<string>(10);

        
public SendMailTest()           
        
{
            _sendTo.Add(
"rockniu@gmail.com");
            _sendTo.Add(
"rockniu@yahoo.cn");
            _copyTo.Add(
"rockniu@live.com");
            _copyTo.Add(
"rockniu@hotmail.com");
        }


        [Test]
        
public void SendWebMail()
        
{
            EmailHelper.SendWebMail(_sendTo, _copyTo, 
"Rock is coming""rock123");
        }


        [Test]
        
public void SendNetMail()
        
{
            EmailHelper.SendNetMail(_sendTo, _copyTo, 
"Rock is coming""rock123");
        }
      
    }

 

posted on 2008-03-14 22:28  阿牛-专注金融行业开发  阅读(539)  评论(0)    收藏  举报