比较完善的asp.net2.0邮件发送类

using System;
using System.Net.Mail;
using System.IO;
/// <summary>
/// Utilities 的摘要说明
/// </summary>
public static class Utilities
{
    static Utilities()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    //读取指定URL地址的HTML,用来以后发送网页用
    public static string ScreenScrapeHtml(string url)
    {
        //读取stream并且对于中文页面防止乱码
        StreamReader reader = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
        string str = reader.ReadToEnd();
        reader.Close();
        return str;
    }
    ///   <summary> 
    ///   发送邮件 
    ///   </summary> 
    ///   <param   name= "server "> smtp地址 </param> 
    ///   <param   name= "username "> 用户名 </param> 
    ///   <param   name= "password "> 密码 </param> 
    ///   <param   name= "from "> 发信人地址 </param> 
    ///   <param   name= "to "> 收信人地址 </param> 
    ///   <param   name= "subject "> 邮件标题 </param> 
    ///   <param   name= "body "> 邮件正文 </param> 
    ///    <param   name= "IsHtml "> 是否是HTML格式的邮件 </param> 
    public static void SendMail(string from, string to, string subject, string body, string server, string username, string password, bool IsHtml)
    {
        //设置SMTP 验证,端口默认为25,如果需要其他请修改
        SmtpClient mailClient = new SmtpClient(server, 25);

        //指定如何发送电子邮件。 
        //Network         电子邮件通过网络发送到   SMTP   服务器。     
        //PickupDirectoryFromIis   将电子邮件复制到挑选目录,然后通过本地   Internet   信息服务   (IIS)   传送。     
        //SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。    
        mailClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

        //创建邮件对象
        MailMessage mailMessage = new MailMessage(from, to, subject, body);
        //定义邮件正文,主题的编码方式
        mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
        mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
        //获取或者设置一个值,该值表示电子邮件正文是否为HTML
        mailMessage.IsBodyHtml = IsHtml;
        //指定邮件的优先级
        mailMessage.Priority = MailPriority.High; ;
        /*
        //   //添加附件 
        //System.Web.Mail.MailAttachment   mailAttachment=new   System.Web.Mail.MailAttachment(@ "f:/att.txt ");   
        if (strFileName != " " && strFileName != null)
        {
            Attachment data = new Attachment(strFileName);
            mailMessage.Attachments.Add(data);
        } 
         */
        //发件人身份验证,否则163   发不了 
        //表示当前登陆用户的默认凭据进行身份验证,并且包含用户名密码
        mailClient.UseDefaultCredentials = true;
        mailClient.Credentials = new System.Net.NetworkCredential(username, password);
        //发送
        mailClient.Send(mailMessage);
    }
    //发送plaintxt
    public static void SendText(string from, string to, string subject, string body, string server, string username, string password)
    {
        SendMail(from, to, server, body, server, username, password, false);
    }
    //发送HTML内容
    public static void SendHtml(string from, string to, string subject, string body, string server, string username, string password)
    {
        SendMail(from, to, server, body, server, username, password, true);
    }
    //发送指定网页
    public static void SendWebUrl(string from, string to, string subject, string server, string username, string password, string url)
    {
        //发送指定网页
        SendHtml(from, to, subject, ScreenScrapeHtml(url), server, username, password);
    }
}

转载于:https://my.oschina.net/smartsmile/blog/815550

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值