asp.net jmail发送邮件代码

用jamil发送邮件要先下载jamil软件安装后才能使用,然后将jamil.dll复制到bin目录下添加引用

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Util;
using System.Web.Mail;
using System.Net.Configuration;
namespace HotTiger.Web
{
    public class SendEmail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailtitle">标题</param>
        /// <param name="mailcontent">内容</param>
        /// <param name="MailTo">发送给谁,支持多个人同时发送,多个人用";"隔开</param>
        /// <returns></returns>
        public static bool Email(string mailtitle, string mailcontent, string MailTo)
        {
            try
            {
                jmail.Message Jmail = new jmail.Message();
                String Subject = mailtitle;
                String body = mailcontent;
                String FromEmail = ConfigurationManager.AppSettings["mailName"];
                String ToEmail = MailTo;
                //Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
                Jmail.Silent = true;
                //Jmail创建的日志,前提loging属性设置为true
                Jmail.Logging = true;
                //字符集,缺省为"US-ASCII"
                Jmail.Charset = "GB2312";
                //信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/HTML"即可。
                Jmail.ContentType = "text/HTML";

                //添加收件人
                string[] arraymail = MailTo.Split(';');
                for (int i = 0; i < arraymail.Length; i++)
                {

                    Jmail.AddRecipientBCC(arraymail[i], "");
                }
                Jmail.From = FromEmail;

                //发件人邮件用户名
                Jmail.MailServerUserName = ConfigurationManager.AppSettings["mailUser"];
                //发件人邮件密码
                Jmail.MailServerPassWord = ConfigurationManager.AppSettings["mailPwd"];
                //设置邮件标题
                Jmail.Subject = Subject;
                //邮件内容
                Jmail.Body = body;
                //Jmail发送的方法
                Jmail.Send(ConfigurationManager.AppSettings["smtp"], false);
                Jmail.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

}

 

   <!--邮箱配置web.config-->
     <add key="smtp" value="smtp.sina.com"/>
    <add key="mailUser" value="xxx"/>
    <add key="mailPwd" value="xxx"/>
    <add key="mailName" value="[email protected]"/>

你可能感兴趣的:(asp.net)