Asp.Net简单的发邮件功能

  今天在收到一个任务,希望能通过程序给用户发邮件,我想这样的功能应该在网上资料很多了,不过自己一直都没有去尝试写过。网上资料查一下,总结一下,代码编写一下,测试一下,很快很顺利的把发邮件的程序写好了。下面分享一下给大家(很多代码都是简写,只记录重点):

  首先,在页面加入一个Button按钮控件:

<asp:Button ID="点击发邮件" runat="server" Text="Button" onclick="Button1_Click"/>

  然后在Button的触发事件里写发邮件要调用到的代码:

  protected void Button1_Click(object sender, EventArgs e)
  {
Email em = new Email();
     em.UserName = "发件人"; //可以自己规定发件人
em.EmailSendUserName="发件人地址";   //可以自己规定邮件地址
em.EmailSendPass="发件人密码";   //可以自己规定密码
     em.Host = "主机地址";   //可以自己规定主机,如Gmail的是smtp.gmail.com

     em.Port = "端口"; //可以自己规定端口,如0x24b
em.EmailToUserName = "收件人地址"; //可以自己规定邮件地址
em.Subject = "邮件标题"; //可以自己规定标题

em.Body = "邮件内容"; //可以自己规定内容
int i = em.SendMailUseGmail();
if (i > 0)
{
Response.Write("ok");
}
else
{
Response.Write("fail");
}
  }

  可以看到上面调用到了Email这个类,所以我们重点的地方就这个Email类,其中,主要调用SendMailUseGmail()这个发邮件的方法:

View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace EmailTest
{
public class Email
{
///<summary>
/// 发送地址
///</summary>
protected string _emailSendUserName = "";
public string EmailSendUserName
{
get
{
return _emailSendUserName;
}
set
{
_emailSendUserName = value;
}
}

///<summary>
/// 发送密码
///</summary>
protected string _emailSendPass = "";
public string EmailSendPass
{
get
{
return _emailSendPass;
}
set
{
_emailSendPass = value;
}
}

///<summary>
/// 接收邮件地址
///</summary>
private string _emailToUserName = "";
public string EmailToUserName
{
get
{
return _emailToUserName;
}
set
{
_emailToUserName = value;
}
}


private string _subject = "";
public string Subject
{
get
{
return _subject;
}
set
{
_subject = value;
}
}

private string _body = "";
public string Body
{
get
{
return _body;
}
set
{
_body = value;
}
}

///<summary>
/// 发送人名称
///</summary>
protected string _userName = "";
public string UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}

protected string _host = "";
public string Host
{
get
{
return _host;
}
set
{
_host = value;
}
}


protected int _port = 0;
public int Port
{
get
{
return _port;
}
set
{
_port = value;
}
}



public int SendMailUseGmail()
{
/*
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");
* msg.To.Add("b@b.com");可以发送给多人
* msg.CC.Add("c@c.com");
* msg.CC.Add("c@c.com");可以抄送给多人
*/
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
//接受人邮件地址 可多个
msg.To.Add(_emailToUserName);
msg.From = new MailAddress(_emailSendUserName, _userName, System.Text.Encoding.UTF8);
/* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
msg.Subject = _subject;//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = _body;//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = true;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(_emailSendUserName, _emailSendPass);

//上述写你的GMail邮箱和密码
client.Host = _host;
client.Port = _port;//Gmail使用的端口
client.EnableSsl = true;//经过ssl加密
//object userState = msg;
int i = 0;
try
{
client.Send(msg);
//client.SendAsync(msg, userState);
i = 1;
}
catch (System.Net.Mail.SmtpException ex)
{
i = -1;
}
return i;
}
}
}

  没有意外的话,以上的代码应该可以实现简单的邮件发送了,如果要完善以上的功能的话,还是有很大的空间的,希望能给大家一些启发。



  

posted @ 2011-11-28 10:45  三歪猫  阅读(453)  评论(3编辑  收藏  举报