邮件发送类

本文介绍了一种通过配置文件实现邮件发送的方法,并提供了详细的代码示例,包括如何设置邮件内容、收件人、抄送人等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

采用配置方式进行实现,已备后查

<!--mail sender config-->
<system.net>
   <mailSettings>
     <smtp from="XX@XX.com">
       <network host="smtp.XXX.com" userName="XX@XX.com" password="XX" defaultCredentials="true" port="25"/>
     </smtp>
   </mailSettings>
</system.net>

ExpandedBlockStart.gifContractedBlock.gifpublic class Mail 

        
const String BccFrom = "XX@XX.com"

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 邮件发送 
        
/// </summary> 
        
/// <param name="mailEntity"></param> 
        
/// <returns></returns> 

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Boolean Send ( MailEntity mailEntity ) 
            Boolean isSucSended 
= true
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
using ( MailMessage mailMessager = new MailMessage () ) 

                    
//是否按Html方式表现邮件正文 
                    mailMessager.IsBodyHtml = true
                    
//邮件等级 
                    mailMessager.Priority = MailPriority.High; 
                    
//邮件主题 
                    mailMessager.Subject = mailEntity.Subject; 
                    
//邮件内容 
                    mailMessager.Body = mailEntity.Body; 
                    
//邮件编码 
                    mailMessager.BodyEncoding = System.Text.Encoding.Default; 
                    
//邮件收件人(列表) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    if ( mailEntity.To.Count > 0 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
foreach ( String ccItem in mailEntity.To ) 
                            mailMessager.To.Add ( ccItem ); 
                        }
 
                    }
 
                    
//抄送人(列表) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    if ( mailEntity.CC.Count > 0 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
foreach ( String ccItem in mailEntity.CC ) 
                            mailMessager.CC.Add ( ccItem ); 
                        }
 
                    }
 
                    
//密送发件人(列表) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    if ( mailEntity.To.Count > 0 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
foreach ( String ccItem in mailEntity.Bcc ) 
                            mailMessager.Bcc.Add ( ccItem ); 
                        }
 
                    }
 
                    
//默认密送发件人 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    if ( mailMessager.From != null ) 
                        mailMessager.Bcc.Add ( mailMessager.From ); 
                    }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
else 
                        mailMessager.Bcc.Add ( BccFrom ); 
                    }
 
                    
//邮件传送通知选项 
                    mailMessager.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; 

                    MailAccountEntity mailAccount 
= GetConfig (); 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    

                        SmtpClient smtpClient 
= new SmtpClient ( mailAccount.Host , mailAccount.Port ); 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        

                            smtpClient.UseDefaultCredentials 
= true
                            smtpClient.Credentials 
= new NetworkCredential ( mailAccount.UserName , mailAccount.Password ); 
                            smtpClient.DeliveryMethod 
= SmtpDeliveryMethod.Network; 
                            smtpClient.Send ( mailMessager ); 
                        }
 
                    }
 
                }
 
            }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( SmtpFailedRecipientException ex ) 
                isSucSended 
= false
            }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch ( System.Exception ex ) 
                isSucSended 
= false
            }
 
            
return isSucSended; 
        }
 

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 获取MailAccount(SmtpAddress,UserName,Password,Port) 
        
/// </summary> 
        
/// <returns></returns> 

ExpandedSubBlockStart.gifContractedSubBlock.gif        private MailAccountEntity GetConfig ( ) 
            MailAccountEntity mailAccount 
= new MailAccountEntity (); 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                
//读取Config的中MailSettings配置段 
                Configuration config = ConfigurationManager.OpenExeConfiguration ( ConfigurationUserLevel.None ); 
                MailSettingsSectionGroup mailSettings 
= NetSectionGroup.GetSectionGroup ( config ).MailSettings; 
                SmtpSection stmpSection 
= mailSettings.Smtp; 
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    mailAccount.Host 
= stmpSection.Network.Host; 
                    mailAccount.UserName 
= stmpSection.Network.UserName; 
                    mailAccount.Password 
= stmpSection.Network.Password; 
                    mailAccount.Port 
= stmpSection.Network.Port; 
                }
 
            }
 
            
return mailAccount; 
        }
 
    }

 

 [Serializable]
ExpandedBlockStart.gifContractedBlock.gif    
public class MailEntity {

        
private String subject;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 邮件主题
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String Subject {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return subject;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                subject 
= value;
            }

        }


        
private String body;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 邮件内容
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String Body {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return body;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                body 
= value;
            }

        }


        
private List<String> to = new List<String> ();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 收件人
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public List<String> To {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return to;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                to 
= value;
            }

        }


        
private List<String> cc = new List<String> ();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 抄送 (CC) 收件人的地址集合。 
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public List<String> CC {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return cc;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                cc 
= value;
            }

        }


        
private List<String> bcc = new List<String> ();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 密件抄送 (BCC) 收件人的地址集合 
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public List<String> Bcc {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return bcc;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                bcc 
= value;
            }

        }

    }

 

[Serializable]
ExpandedBlockStart.gifContractedBlock.gif    
public class MailAccountEntity {
        
private String userName;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 邮箱用户名
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String UserName {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return userName;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                userName 
= value;
            }

        }


        
private String password;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 邮箱密码
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String Password {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return password;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                password 
= value;
            }

        }


        
private Int32 port;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 邮箱port
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Int32 Port {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return port;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                port 
= value;
            }

        }


        
private String host;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 邮箱host,Smtp
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public String Host {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
return host;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set {
                host 
= value;
            }

        }

    }

转载于:https://www.cnblogs.com/RuiLei/archive/2009/05/27/1491152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值