您的位置:首页 > 编程语言 > ASP

ASP.Net使用jmail和System.Net.Mail发送邮件

2015-06-10 17:23 921 查看
ASP.Net使用jmail发送邮件

环境

1.服务器上有jmail.dll文件(我用的是Ver4.5),并成功注册:C:\regsvr32 D:\jmail.dll

2.VS2010中添加引用

代码如下:

using System;

namespace WebApplication2

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Button1_Click(object sender, EventArgs e)

        {

            SendEmail();

        }

        public void SendEmail()

        {

            jmail.Message Jmail = new jmail.Message();

            Jmail.ContentType = "text/html";

            Jmail.Silent = false;

            Jmail.Logging = true;

            Jmail.Charset = "GB2312";

            Jmail.AddRecipient("xxx@163.com", "显示名");

            //群发添加Jmail.AddRecipient("***@***", "显示名");

            Jmail.From = "FromEmail";

            Jmail.FromName = "Name";

            Jmail.MailServerUserName = "UserName";//注是要写全称 并且不要带有下划线的用户名

            Jmail.MailServerPassWord = "PassWord";//不要带有下划线的密码

            

            Jmail.Subject = "主题";

            //Jmail.Body = "内容";//文本格式正文

            //html格式正文

            Jmail.AppendHTML("<html><table  bgcolor=#FFFFCE border=1>");

            Jmail.AppendHTML("<tr><td>正文</td></tr> ");

            Jmail.AppendHTML("</table></html>");

            

            Jmail.Send("stmp.***.com", false);

            Jmail.Close();

        }

    }

}

也可以在程序中设置以下内容,从web.config 的AppSettings获取你设置的账号密码和发送服务器等信息

   String UserName = ConfigurationManager.AppSettings["UserName"].ToString();

   String PassWord = ConfigurationManager.AppSettings["PassWord"].ToString();

   String SendServer = ConfigurationManager.AppSettings["SendServer"].ToString();

web.config中

<appSettings >

    <!-- 你邮箱的账号密码和邮箱的发送服务器等信息 -->

    <add key="UserName" value="abc123" />

    <add key="PassWord" value="mima" />

    <add key="SendServer" value="smtp.abc.com" />

</appSettings>

*************************************************************

ASP.Net使用System.Net.Mail发送邮件

using System;

using System.Net.Mail;

namespace WebApplication2

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Button1_Click(object sender, EventArgs e)

        {

            SendEmail();

        }

        

        public void SendEmail()

        {

            MailMessage newEmail = new MailMessage();

            newEmail.From = new MailAddress("***@163.com","显示名");//发送方邮件

            //发送对象,可群发

            newEmail.To.Add(new MailAddress("***@***"));  //接收方邮箱一

            //newEmail.To.Add(new MailAddress("132@hotmail.com"));  //接收方邮箱二

            newEmail.Subject = "测试邮件";  //标题

            newEmail.Body = "<p><b>这里是内容</b></p>"; //内容,html格式,也可以是普通文本格式;

            newEmail.Body += "<p><b>这里是内容</b></p>";

            //附件

            //Attachment MsgAttach = new Attachment(this.FileUpload1.PostedFile.FileName);//可通过一个FileUpload地址获取附件地址

            //newEmail.Attachments.Add(MsgAttach);

            #region Deployment

            newEmail.IsBodyHtml = true;                //是否支持html

            newEmail.Priority = MailPriority.Normal;  //优先级

            #endregion

            //发送方服务器信息

            SmtpClient smtpClient = new SmtpClient();

            smtpClient.UseDefaultCredentials = true;//安全认证

            smtpClient.Credentials = new System.Net.NetworkCredential("user", "password");//安全认证

            smtpClient.Host = "stmp.163.com"; //主机

            //smtpClient.Send(newEmail);   //同步发送,程序将被阻塞

            #region 异步发送, 会进入回调函数SendCompletedCallback,来判断发送是否成功

            smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);//回调函数

            string userState = "测试";

            smtpClient.SendAsync(newEmail, userState);//异步发送, WEB页面需添加<% @ pageAsync="true"%>

            #endregion

        }

        private static void SendCompletedCallback(object sender, System.ComponentModel.AsyncCompletedEventArgs e)

        {

            if (e.Cancelled)  //邮件发送被取消

            {

            }

            if (e.Error != null)   //邮件发送失败

            {

            }

            else   //发送成功

            {

            }

        }

    }
}

如果上述用vb.net语言编写,其委托调用时,将

smtpClient.SendCompleted += New SendCompletedEventHandler(SendCompletedCallback) '//回调函数

修改为

AddHandler smtpClient.SendCompleted, AddressOf SendCompletedCallback

调用的函数改为

Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

        If (e.Cancelled) Then

        End If

        If (e.Error IsNot Nothing) Then '邮件发送失败

        Else '//发送成功

        End If

End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# Mail jmail