简洁版发送:
//web.config ---文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration> <appSettings> <add key="email" value="[email protected]"/> <add key="password" value="1c2s1/2h"/>
</appSettings> <system.web> <compilation debug="false" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web>
</configuration>
//Defult.aspx ---文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form /> </form> </body> </html>
//Defult.aspx.cs ---文件 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//发送邮件
SmtpClient smtp = new SmtpClient("smtp.163.com");
//设置邮件类型(1.发送者 2.接受者 3.邮件体 4.登录信息)
//1.
string address = System.Configuration.ConfigurationManager.AppSettings["email"].ToString();
MailAddress from = new MailAddress(address);
//2.
MailAddress to = new MailAddress(tetTo.Text);
//3.
MailMessage mail = new MailMessage(from,to);
mail.Subject = tetSubject.Text;
mail.Body = tetBody.Text;
//4.
string pwd = System.Configuration.ConfigurationManager.AppSettings["password"].ToString();
NetworkCredential cred = new NetworkCredential(address,pwd);
smtp.Credentials = cred;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//发送
smtp.Send(mail);
}
}
二、完全版发送接收:(在这个方法中需要用到OpenPop.dll文件 链接:http://pan.baidu.com/s/1bnwjWJH)
//default.aspx ---文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form ) %>) </a> </li> </ItemTemplate> </asp:Repeater>
</div> </form> </body> </html>
//default.aspx.cs ---文件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using OpenPop.Pop3; using OpenPop.Mime; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { List<MailItem> list = new List<MailItem>();
//实例化pop3访问对象 Pop3Client pop3 = new Pop3Client(); //链接到邮件服务器 pop3.Connect("pop3.sina.com",110,false); //身份验证 pop3.Authenticate("[email protected]","abcde12345");
//读邮件列表 //1.获取邮件的个数 int count = pop3.GetMessageCount(); //2.遍历显示出来 for (int i = 1; i <= count; i++) { Message msg = pop3.GetMessage(i); MailItem data = new MailItem(); data.NO = i; data.FromAddress = msg.Headers.From.Address;//发送者的邮箱地址 data.FromSender = msg.Headers.From.DisplayName;//发送者的名子 data.Date = msg.Headers.DateSent;//邮件的发送时间 data.Subject = msg.Headers.Subject;//邮件的主题 data.Body = msg.MessagePart.GetBodyAsText(); list.Add(data); } //断开链接 pop3.Disconnect();
//绑定显示 Repeater1.DataSource = list; Repeater1.DataBind(); } }
public class MailItem { public int NO { get; set; } public string FromSender { get; set; } public string FromAddress { get; set; } public DateTime Date { get; set; } public string Subject { get; set; } public string Body { get; set; } }
//details.aspx ---文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="Details" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form ></asp:Label> <br />
</div> </form> </body> </html>
//details.aspx.cs ---文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPop.Pop3;
using OpenPop.Mime;
public partial class Details : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["id"] != null)
{
//获取邮件代号
int no = Convert.ToInt32(Request["id"]);
//根据邮件代号查找邮件
Pop3Client pop3 = new Pop3Client();
pop3.Connect("pop3.sina.com",110,false);
pop3.Authenticate("[email protected]","abcde12345");
Message msg =pop3.GetMessage(no);
//显示
lblSubject.Text = msg.Headers.Subject;
lblSender.Text = msg.Headers.From.DisplayName + msg.Headers.From.Address;
lblDate.Text = msg.Headers.DateSent.ToString();
lblBody.Text = msg.MessagePart.GetBodyAsText();
//断开链接
pop3.Disconnect();
}
}
}
一、邮件发送:.NET中自带SMTP邮件发送的类——SmtpClient
第一步:导入命名空间:
using System.Net.Mail; using System.Net;
第二步:编写邮件发送代码:
//1.实例化发邮件的类 SmtpClient smtp = new SmtpClient("smtp.sina.com");
//2.构建邮件对象。邮件对象常见的一般包括:发送邮箱,接收邮箱,标题,内容四大部分
MailAddress from = new MailAddress("[email protected]"); //发送者的邮箱地址 MailAddress to = new MailAddress("[email protected]"); //接收者的邮箱地址 MailMessage mail = new MailMessage(from, to); //实例化邮件对象 mail.Subject = txtSubject.Text; //邮件的标题 mail.Body = txtBody.Text; //邮件的内容
//3.设置邮箱登录验证凭据
NetworkCredential cred = new NetworkCredential("发送者的邮箱用户名","发送者的邮箱密码"); //创建凭据对象 smtp.Credentials = cred; //把凭据对象赋给邮件发送类
//4.发送
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(mail);
二、邮件接收:.NET中没有POP3邮件接收的类,邮件的内容和格式比复杂,手动写代码进行解析很麻烦,也容易出错,开发中我们可以借助第三方插件来实现。
OpenPOP.NET插件的地址:http://sourceforge.net/projects/hpop/
第一步:添加引用:OpenPop.dll
第二步:导入命名空间:
using OpenPop.Pop3;
using OpenPop.Mime;
第三步:编写邮件实体类:
public class MailItem
{
public int NO { get; set; }
public string FromSender { get; set; }
public string FromAddress { get; set; }
public DateTime Date { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
第四步:编写邮件读取代码
protected void Page_Load(object sender, EventArgs e)
{
List<MailItem> list = new List<MailItem>();
//实例化pop3访问对象
Pop3Client pop3 = new Pop3Client();
//链接到邮件服务器
pop3.Connect("pop3.sina.com",110,false);
//身份验证
pop3.Authenticate("[email protected]","abcde12345");
//读邮件列表
//1.获取邮件的个数
int count = pop3.GetMessageCount();
//2.遍历显示出来
for (int i = 1; i <= count; i++)
{
Message msg = pop3.GetMessage(i);
MailItem data = new MailItem();
data.NO = i;
data.FromAddress = msg.Headers.From.Address;//发送者的邮箱地址
data.FromSender = msg.Headers.From.DisplayName;//发送者的名子
data.Date = msg.Headers.DateSent;//邮件的发送时间
data.Subject = msg.Headers.Subject;//邮件的主题
data.Body = msg.MessagePart.GetBodyAsText();
list.Add(data);
}
//断开链接
pop3.Disconnect();
//绑定显示
Repeater1.DataSource = list;
Repeater1.DataBind();
}
}
|
请发表评论