.NET中用SMTP发邮件的两中方法总结

方法一

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 ="[email protected]" />
        
< 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(
"[email protected]");
            _sendTo.Add(
"[email protected]");
            _copyTo.Add(
"[email protected]");
            _copyTo.Add(
"[email protected]");
        }


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


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

 

你可能感兴趣的:(.net)