首页  :: 新随笔  :: 订阅 订阅  :: 管理

C#中自定义邮件发送类(支持多人多附件)

Posted on 2007-07-17 14:20  礼拜一  阅读(3220)  评论(3编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace ConsoleApplication1
{
    
/// <summary>
    
/// 发送邮件类 的摘要说明
    
/// </summary>

    class SendMail
    
{
        
数据成员

        
构造函数

        
数据属性

        
/// <summary>
        
/// 发送邮件
        
/// </summary>
        
/// <returns>发送是否成功</returns>

        public bool Send()
        
{
            
try
            
{
                
//获取所有的收件人地址
                char[] ch = ',' };
                
string[] address = m_To.Split(ch);
                MailAddressCollection allAddress 
= new MailAddressCollection();
                
for (int i = 0; i < address.Length; i++)
                
{
                    
//收件人地址
                    MailAddress toAddress = new MailAddress(address[i]);
                    allAddress.Add(toAddress);

                    
//发件人地址
                    MailAddress fromAddress = new MailAddress(m_From);

                    
//定义邮件消息
                    MailMessage msg = new MailMessage(fromAddress, toAddress);
                    
//邮件标题
                    msg.Subject = m_Subject;
                    
//邮件正文
                    msg.Body = m_Body;

                    
//获取所有邮件附件
                    char[] cr = ';' };
                    
string[] file = m_File.Split(cr);
                    
for (int n = 0; n < file.Length; n++)
                    
{
                        
if (file[n] != "")
                        
{
                            
//附件对象
                            Attachment data = new Attachment(file[n], MediaTypeNames.Application.Octet);
                            
//附件资料
                            ContentDisposition disposition = data.ContentDisposition;
                            disposition.CreationDate 
= System.IO.File.GetCreationTime(file[n]);
                            disposition.ModificationDate 
= System.IO.File.GetLastWriteTime(file[n]);
                            disposition.ReadDate 
= System.IO.File.GetLastAccessTime(file[n]);
                            
//加入邮件附件
                            msg.Attachments.Add(data);
                        }

                    }


                    
//使用简单邮件传输协议来传送邮件
                    SmtpClient sendMail = new SmtpClient();
                    
//发送邮件的服务器名或地址
                    sendMail.Host = m_Host;
                    
//验证发件人的身份
                    sendMail.Credentials = new NetworkCredential(m_UserName, m_Password);
                    
//处理待发送邮件的方法
                    sendMail.DeliveryMethod = SmtpDeliveryMethod.Network;

                    
//发送邮件
                    sendMail.Send(msg);                  
                }

                
return true;
            }

            
catch (Exception ex)
            
{
                
return false;
            }

        }

    }

}