c#邮箱发送和接收

简洁版发送:

//web.config ---文件

<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<appSettings>
<add key="email" value="15054590260@163.com"/>
<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 id="form1" runat="server">
邮件发送<br />
<br />
接收人:<asp:TextBox ID="tetTo" runat="server" Width="348px"></asp:TextBox>
<br />
<br />
邮件标题:<asp:TextBox ID="tetSubject" runat="server" Width="330px"></asp:TextBox>
<br />
<br />
邮件内容:<br />
<asp:TextBox ID="tetBody" runat="server" Height="164px" Width="408px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="发送" OnClick="Button1_Click" />
</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 id="form1" runat="server">
<div>

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
<ItemTemplate>
<li>
<a href="Details.aspx?id=<%# Eval("NO") %>">
<%# Eval("Subject") %>
<%--<%# ((DateTime)Eval("Date")).ToString("yyyy年MM月dd日hh时mm分ss秒") %>--%>
<% Eval("Date","{0:yyyy年MM月dd日hh时mm分ss秒}") %>
<%# Eval("FromSender") %>(<%# Eval("FromAddress") %>)
</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("z1n10527@sina.com","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 id="form1" runat="server">
<div>

标题:<asp:Label ID="lblSubject" runat="server" Text="Label"></asp:Label>
<br />
发送者:<asp:Label ID="lblSender" runat="server" Text="Label"></asp:Label>
<br />
时间:<asp:Label ID="lblDate" runat="server" Text="Label"></asp:Label>
<br />
内容:<br />
<asp:Label ID="lblBody" runat="server" Text="Label"></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("z1n10527@sina.com","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("z1n10527@sina.com"); //发送者的邮箱地址
MailAddress to = new MailAddress("z1n10426@sina.com"); //接收者的邮箱地址
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("z1n10527@sina.com","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();
}
}
上一篇:Asp.Net Web Api中使用Swagger


下一篇:git revert 与 git reset