C#发送邮件

最近公司让研究一下发送邮件..试过VB C#感觉还是C#较比稳定些.下面将代码共享一下 2.0 


 

 


public   class  SendMail 
     

         
private string _host; 
         
public string Host 
         

             
get return _host; } 
             
set { _host = value; } 
         }
 
         
private int _port; 
         
public int Port 
         

             
get return _port; } 
             
set { _port = value; } 
         }
 
         
private string _smtpUsername; 
         
public string SmtpUsername 
         

             
get return _smtpUsername; } 
             
set { _smtpUsername = value; } 
         }
 
         
private string _smtpPassword; 
         
public string SmtpPassword 
         

             
get return _smtpPassword; } 
             
set { _smtpPassword = value; } 
         }
 
         
public void Send(string from, string to, string subject, string body, string[] cc, string[] bcc) 
         

             
// Create mail message 
             MailMessage message = new MailMessage(from, to, subject, body); 
             message.BodyEncoding 
= Encoding.GetEncoding(936); 
             
if (cc != null && cc.Length > 0
             

                 
foreach (string ccAddress in cc) 
                 

                     message.CC.Add(
new MailAddress(ccAddress)); 
                 }
 
             }
 
             
if (bcc != null && bcc.Length > 0
             

                 
foreach (string bccAddress in bcc) 
                 

                     message.Bcc.Add(
new MailAddress(bccAddress)); 
                 }
 
             }
 
             
// Send email 
             SmtpClient client = new SmtpClient(this._host, 25); 
             
if (!String.IsNullOrEmpty(this._smtpUsername) && !String.IsNullOrEmpty(this._smtpPassword)) 
             

                 client.Credentials 
= new NetworkCredential(this._smtpUsername, this._smtpPassword); 
             }
 
             client.EnableSsl 
= false
             client.Send(message);             
         }
 

调用: 
try 
             

                 SendMail mail 
= new SendMail(); 
                 mail.Host 
= this.txt_MailServer.Text;//服务器smtp地址 
                 mail.SmtpUsername = this.txt_UserName.Text;//登陆用户名 
                 mail.SmtpPassword = this.txt_Pass.Text;//登录密码 
                 mail.Send(this.txt_MailAdder.Text, this.txt_toMail.Text, this.txt_title.Text, this.txt_body.Text, nullnull);//发件人地址,收件人地址,标题,内容,其他,其他 
             }
 
             
catch (Exception ex) 
             

               
//可以捕获异常 
             }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值