.Net 发送邮件 (不用jmail)
作者:朱尚 / 日期:2014-06-12 / 分类:Asp.Net / 浏览:1982
using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;

/// <summary>
/// 发送邮件
/// </summary>
namespace Basic.Tools
{
    public class Mail
    {
        /// <summary>
        /// 发送邮件(smtp服务器/发送邮箱/邮箱密码/接收邮箱/发件人名称/邮件标题/邮件内容)
        /// </summary>
        /// <param name="_Host"></param>
        /// <param name="_from"></param>
        /// <param name="_pwd"></param>
        /// <param name="_tomail"></param>
        /// <param name="_senderDisplayName"></param>
        /// <param name="_Subject"></param>
        /// <param name="_Body"></param>
        /// <returns></returns>
        public static bool Send(string _Host, string _from, string _pwd, string _tomail, string _senderName, string _Subject, string _Body)
        {
            bool Result = false;
            //创建smtpclient对象   
            System.Net.Mail.SmtpClient client = new SmtpClient();
            client.Host = _Host;//163的smtp服务器是 smtp.163.com   
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(_from, _pwd);

            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            MailAddress mailfrom = new MailAddress(_from, _senderName, encoding);//发件人邮箱地址,名称,编码UTF8   
            MailAddress mailto = new MailAddress(_tomail, "A", encoding);//收件人邮箱地址,名称,编码UTF8   
            //创建mailMessage对象   
            System.Net.Mail.MailMessage message = new MailMessage(mailfrom, mailto);
            message.Subject = _Subject;
            //正文默认格式为html   
            message.Body = _Body;
            message.IsBodyHtml = true;
            message.BodyEncoding = encoding;
            message.SubjectEncoding = encoding;
            //message.HeadersEncoding = encoding;

            try
            {
                client.Send(message);
                Result = true;
            }
            catch (Exception ex)
            {
                string e = ex.Message.ToString();
                Result = false;
            }
            return Result;
        }
    }
}
上一篇:验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 配置指定了相同的 validationKey 和验证算法。
下一篇:Sys.WebForms.PageRequestManagerTimeoutException: The server request timed out
本文标签: 发邮件 邮箱 Mail
本文链接:http://www.banzhuan.net/detail/244