ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ASP.NET之邮件发送

ASP.NET之邮件发送

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-01-16 15:43:45 0 删除 编辑

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;

namespace Maticsoft.BLL
{
    public class SendMail
    {
        private MailMessage mailMessage;

        private SmtpClient smtpClient;

        private string password;//发件人密码          

        //开始收邮件功能
        public void GetMail(string To)
        {

        }
        //结束收邮件功能

        ///

  

        /// 处审核后类的实例  

        ///

  

        /// 收件人地址  

        /// 发件人地址  

        /// 邮件正文  

        /// 邮件的主题  

        /// 发件人密码  

        public SendMail(string To, string From, string Body, string Title, string Password)
        {

            mailMessage = new MailMessage();

            mailMessage.To.Add(To);

            mailMessage.From = new System.Net.Mail.MailAddress(From);

            mailMessage.Subject = Title;

            mailMessage.Body = Body;

            mailMessage.IsBodyHtml = true;

            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

            mailMessage.Priority = System.Net.Mail.MailPriority.Normal;

            this.password = Password;

        }

        ///

  

        /// 添加附件  

        ///

  

        public void Attachments(string Path)
        {

            string[] path = Path.Split(',');

            Attachment data;

            ContentDisposition disposition;

            for (int i = 0; i < path.Length; i++)
            {

                data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件  

                disposition = data.ContentDisposition;

                disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期  

                disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期  

                disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期  

                mailMessage.Attachments.Add(data);//添加到附件中  

            }

        }

        ///

  

        /// 异步发送邮件  

        ///

  

        ///   

        public void SendAsync(SendCompletedEventHandler CompletedMethod)
        {

            if (mailMessage != null)
            {

                smtpClient = new SmtpClient();

                smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据  

                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                smtpClient.Host = "smtp." + mailMessage.From.Host;

                smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件  

                smtpClient.SendAsync(mailMessage, mailMessage.Body);

            }

        }

 

        ///

  

        /// 发送邮件  

        ///

  

        public string Send(string fwqdz)
        {
            string EmailAddress = mailMessage.To.ToString();
            try
            {
                if (mailMessage != null)
                {
                    smtpClient = new SmtpClient();

                    //smtpClient.EnableSsl = true;//启用安全连接

                    smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据  

                    smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                    smtpClient.Host = fwqdz; //服务器发送地址比如:"smtp.sina.com";

                    smtpClient.Port = 25;//邮件端口

                    smtpClient.Send(mailMessage);

                    return "";
                }
                else
                {
                    return EmailAddress;
                }
            }
            catch (Exception ex)
            {
                return EmailAddress + "," + ex.Message;//将发送失败的收件人和异常信息Return
            }
        }
    }
}

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-539842/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5346105