.Net写的Mail Util(C#)

来源:互联网 发布:92game源码破解 编辑:程序博客网 时间:2024/05/18 15:56
前两天用.Net写了个发送mail的util,贴上来。刚接触C#,呵呵。。。。
.net 1.1的。.net 2.0里面关于Mail的API有所变动。 .code { font-size: 12; font-family: "lucida console", sans-serif; background-color: #E1E1E1; }.com { color: #00DD00; }.nlit { color: #008800; }.slit { color: #880000; }.kw { color: #0000AA; }.norm { color: #000000; }.pp { color: #888800; }



using System;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Mail;
namespace Izhufu.utils{
  
/// <summary>
  /// Summary description for SMTPClient.
  /// SMTPClient is just used for SMTPMail
  /// </summary>
  
public class SMTPClient {
    
private readonly string DELIMITER=";";
    
private MailMessage message;
    
private string[] s_cc;
    
private string[] s_bcc;
    
private int s_port;
    
private bool s_needauth;
    
private string s_username;
    
private string s_password;
    
//constructor
    
public SMTPClient (string from, string[] to, string subject, string server) {
      message=
new MailMessage();
      message.From=from;
      message.To=concatMailAddress(to, DELIMITER);
      message.Subject=subject;
      SmtpMail.SmtpServer=server;
    }

    
//setter for mail server port
    
public void SetPort (int port){
      
this.s_port=port;
    }

    
//setter for mail priority, for example: SetPrioprity(MailPriority.HIGH)
    
public void SetPriority (MailPriority priority){
      message.Priority=priority;
    }

    
//setter for mail server auth configuration
    
public void SetNeedAuth (bool needauth){
      
this.s_needauth=needauth;
    }

    
//setter for mail server user name
    
public void SetUserName (string username){
      
this.s_username=username;
    }

    
//setter for mail server password
    
public void SetPassword (string pwd){
      
this.s_password=pwd;
    }

    
//setter for mail body format html?or text  for exampel: SetFormat(MailFormat.Text)
    
public void SetFormat (MailFormat format){
      message.BodyFormat=format;
    }

    
//setter for mail body
    
public void SetBody (string body){
      message.Body=body;
    }

    
//setter for mail body encoding, for example: SetEncoding(Encoding.ASCII
    
public void SetEncoding (Encoding encoding){
      message.BodyEncoding=encoding;
    }

    
//add attachment to mail
    
public void AddAttachment (MailAttachment attachement){
      message.Attachments.Add(attachement);
    }

    
//set cc 
    
public void SetCC (string[] cc){
      
this.s_cc=cc;
    }

    
//set bcc
    
public void SetBcc (string[] bcc){
      
this.s_bcc=bcc;
    }

    
//private method to concat the mail address with specified delimiter
    
private string concatMailAddress (string[] addresses, string delimiter){
      
if (addresses==nullreturn null;
      
if (addresses.Length==1return addresses[0];
      
string concated="";
      
for (int i=0; i<addresses.Length-1; i++) {
        concated+=addresses[i]+delimiter;
      }
      concated+=addresses[addresses.Length-
1];
      
return concated;
    }

    
//send the mail, if this mail is sent successfully return true, otherwise return false
    
public bool SendMail (){
      
if (s_cc!=null && s_cc.Length>0) {
        message.Cc=concatMailAddress(s_cc, DELIMITER);
      }
      
if (s_bcc!=null && s_bcc.Length>0) {
        concatMailAddress(s_bcc, DELIMITER);
      }
      
if (s_needauth && s_username!=null && s_password!=null) {
        message.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1");
        message.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendusername", s_username);
        message.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendpassword", s_password);
      }
      
try {
        SmtpMail.Send(message);
      }
      
catch {
        
return false;
      }
      
return true;
    }

  }

//End of SMTPClient
}
原创粉丝点击