C# 发送邮件的Helper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Timers;
using System.Xml;
using System.Net;
namespace TEST.SUBUNIT
{
    public class EmailHelper
    {
        public MailMessage mailMessage(string fromAddress,string desemailuser, string toAddress, string emailSubject, string emailText)  //邮件的发送收者,支持群发,多个地址之间用 半角逗号 分开
        {
            MailMessage mm = new MailMessage();
            mm.To.Add("abc@qq.com");
            //mm.To.Add(toAddress);//暂时屏蔽
            ////抄送
              mm.CC.Add(new MailAddress("11@qq.com", "张三", Encoding.GetEncoding(936)));
            mm.CC.Add(new MailAddress("22@qq.com", "李四", Encoding.GetEncoding(936)));
            mm.CC.Add(new MailAddress("33@126.com", "王五", Encoding.GetEncoding(936)));
          //mm.Bcc.Add("44@qq.com,44@qq.com");

            mm.From = new MailAddress(fromAddress, desemailuser.Length>0?desemailuser:fromAddress, Encoding.GetEncoding(936));
            mm.SubjectEncoding = Encoding.GetEncoding(936);//这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
              mm.Subject = emailSubject; //邮件标题
              mm.IsBodyHtml = true; //邮件正文是否是HTML格式
            
              mm.BodyEncoding = Encoding.GetEncoding(936); //邮件正文的编码, 设置不正确, 接收者会收到乱码
            //-------------------------------------------------------------------------
            emailText += "这是要正确发送的邮箱:" + toAddress;
            mm.Body = emailText;//邮件正文
              mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
            //mm.Attachments.Add(new Attachment(@"d:a.doc",System.Net.Mime.MediaTypeNames.Application.Rtf));
            ////第二个参数,表示附件的文件类型,可以不用指定
            return mm;
        }

        public bool sendEmail(string fromemailaddress, string toemailaddress,string emailtitle,string emailcontent,string host,string port,string fromemailuser,string desemailuser,string fromemailpass)
        {
            bool b=false;
           string message=string.Empty;
            MailMessage mailessage =this.mailMessage(fromemailaddress,desemailuser,toemailaddress,emailtitle,emailcontent);
            SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
            smtp.EnableSsl = false;//smtp服务器是否启用SSL加密
              smtp.Host = host; //指定 smtp 服务器地址
              smtp.Port = int.Parse(port);             //指定 smtp 服务器的端口,默认是25
            smtp.Credentials = new NetworkCredential(fromemailuser, fromemailpass);//认证
            try
            {
                smtp.Send(mailessage);
                b = True;
                message= "发送成功!";
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                b = False;
                message= "发送失败!";
            }
            return b;
        }

    }
}

 

编辑器加载中...

posted @ 2012-04-02 17:42  Tom Fan  阅读(934)  评论(0编辑  收藏  举报