邮件发送类,支持Gmail,使用简单方便

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace LZ2007.Function
{
    
/// <summary>
    
/// 邮件发送类
    
/// <CreateDate>2007/07/02</CreateDate>
    
/// <ModifyDate>2007/07/02</ModifyDate>
    
/// <Author>张怡冰</Author>
    
/// </summary>

    public class Mail
    
{
        
public Mail(string host, int port, string smtpUsername, string smtpPassword)
        
{
            _host 
= host;
            _port 
= port;
            _smtpUsername 
= smtpUsername;
            _smtpPassword 
= smtpPassword;
        }


        
public Mail() { }

        
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 SendMail(string from, string to, string subject, string body, bool ssl, string[] cc, string[] bcc)
        
{
            
// Create mail message
            MailMessage message = new MailMessage(from, to, subject, body);
            message.BodyEncoding 
= Encoding.GetEncoding(936);
            message.IsBodyHtml 
= true;
            
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, this._port);
            
if (!String.IsNullOrEmpty(this._smtpUsername) && !String.IsNullOrEmpty(this._smtpPassword))
            
{
                client.Credentials 
= new NetworkCredential(this._smtpUsername, this._smtpPassword);
            }

            
if (ssl)
            
{
                client.EnableSsl 
= true;
            }

            client.Send(message);
        }

    }

}
posted on 2007-08-01 15:56  雨中流泪  阅读(534)  评论(0编辑  收藏  举报
友情链接:
http://www.52cookbook.com ---你的美食天地