代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
asp.net发送接收邮件以及附件
作者:
dezai
/ 发布于
2014/7/28
/
278
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using jmail; using System.IO; using System.Net.Mail; using System.Net; using System.Net.Mime; public partial class getemail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void sendEmail_Click(object sender, EventArgs e) { #region SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Credentials = new NetworkCredential("likeshan168@gmail.com", "likeshannihao168"); MailMessage mail = new MailMessage("likeshan168@gmail.com", emailRec.Text.Trim()); mail.Subject = subject.Text.Trim(); mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = emailbody.Text.Trim(); mail.BodyEncoding = System.Text.Encoding.UTF8; smtpClient.EnableSsl = true; string attafile = fileUpload.PostedFile.FileName; if (!string.IsNullOrEmpty(attafile))//这里是添加附件的作用 { #region//方法一 //string filename = Path.GetFileName(attafile); //fileUpload.PostedFile.SaveAs(Server.MapPath(filename)); //System.Net.Mail.Attachment atta = new System.Net.Mail.Attachment(Server.MapPath(filename)); //mail.Attachments.Add(atta); #endregion #region//方法二 //string filepath = Server.MapPath("~/UploadFile/Files"); //string extName = Path.GetExtension(attafile); //if (!Directory.Exists(filepath)) //{ // Directory.CreateDirectory(filepath); //} //filepath = filepath + "\" + DateTime.Now.ToString("yyyyMMddhhMMssffff") + extName; //fileUpload.PostedFile.SaveAs(filepath); //System.Net.Mail.Attachment atta = null; //if (extName == ".zip" || extName == ".rar") //{ // atta = new System.Net.Mail.Attachment(filepath, MediaTypeNames.Application.Zip); //} //else //{ // atta = new System.Net.Mail.Attachment(filepath, MediaTypeNames.Application.Octet); //} #endregion #region//方法三 string filepath = Server.MapPath("~/UploadFile/Files"); string extName = Path.GetExtension(attafile); if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); } filepath = filepath + "\" + DateTime.Now.ToString("yyyyMMddhhMMssffff") + extName; fileUpload.PostedFile.SaveAs(filepath); System.Net.Mail.Attachment atta = null; if (extName == ".zip" || extName == ".rar") { atta = new System.Net.Mail.Attachment(new FileStream(filepath, FileMode.Open, FileAccess.Read), attafile, MediaTypeNames.Application.Zip); } else { atta = new System.Net.Mail.Attachment(new FileStream(filepath, FileMode.Open, FileAccess.Read), attafile, MediaTypeNames.Application.Octet); } mail.Attachments.Add(atta); #endregion } mail.Priority = MailPriority.Normal; try { smtpClient.Send(mail); ClientScript.RegisterClientScriptBlock(Page.GetType(), "tip1", "alert('发送成功!')", true); } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(Page.GetType(), "tip2", "alert('" + ex.Message + "!')", true); } #endregion } } 接受邮件的代码 <%@ WebHandler Language="C#" Class="getEmail" %> using System; using System.Web; using jmail; using System.Text; public class getEmail : IHttpHandler { public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; POP3Class popClient = new POP3Class(); try { popClient.Connect("likeshan168@163.com", "likeshannihao168", "pop.163.com", 110); if (popClient != null) { if (popClient.Count > 0)//收件数目 { StringBuilder sb = new StringBuilder(); Message msg = null; for (int i = 0, len = popClient.Count; i < len; i++) { msg = popClient.Messages[i + 1]; string msgid = popClient.GetMessageUID(i + 1);//029DCA8D131AA92586C8FB6CB61B627B0E00000000000001(邮件在服务器中的唯一标识) int c = popClient.GetLastUnreadMessage(); msg.ISOEncodeHeaders = false; sb.Append("时间:" + msg.Date.ToString() + ""); sb.Append("发件人:" + msg.From + ""); string substr = msg.Headers.GetHeader("Subject");//只要有中文就会进行base64的编码,如果是英文的话,就不会进行base64的编码 if (substr.Contains("utf-8")) { substr = DecodeStr(substr.Split('?')[3], "utf-8");//=?utf-8?B?5pyJ5LiA5Liq5rWL6K+V?= } if (substr.Contains("gbk") || substr.Contains("GBK") || substr.Contains("GB2312")) { substr = DecodeStr(substr.Split('?')[3], "gbk"); } sb.Append("主题:" + substr + ""); sb.Append("内容:" + msg.Body + ""); int attaCount = msg.Attachments.Count; for (int j = 0; j < attaCount; j++) { sb.Append("附件(" + (j + 1).ToString() + "):" + msg.Attachments[j].Name + ""); } } context.Response.Write(sb.ToString()); } } } catch (Exception ex) { context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } } public static string base64GbkDecode(string data) { string decode = ""; byte[] bytes = Convert.FromBase64String(data);//先经过base64解码,在经过gbk2312解码 try { decode = Encoding.GetEncoding("gb2312").GetString(bytes); } catch (Exception ex1) { return "Error in base64Encode" + ex1.Message; } return decode; } public static string base64Utf8Decode(string data) { string result = ""; try { System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decode = encoder.GetDecoder();//获取解码器 byte[] todecode_byte = Convert.FromBase64String(data);//先经过base64解码,在经过utf-8解码 int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);//多少个字符 char[] decoded_char = new char[charCount];//字符字符数组 utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);//解码到字符数组 result = new String(decoded_char); } catch (Exception e) { return "Error in base64Encode" + e.Message; } return result; } //base64解码 public static string DecodeStr(string allstr, string code) { //形如=?...?=是结束开始的标志 //=?utf-8?B?5rWL6K+V5o6l5pS25pys6YKu5Lu26L+Z5piv5Li76aKY?= //=?gbk?B?suLK1L3TytXN4rK/08q8/tXiuPbKx9b3zOU=?= //返回的字符串 string str = ""; if (code == "gbk") { str = base64GbkDecode(allstr); } else if (code == "utf-8") { str = base64Utf8Decode(allstr); } return str; } // string subjectStr = popMail.Messages[i].Headers.GetHeader("Subject"); //subjectstr = DecodeStr(subjectallstr.Split('?')[3], “utf-8”); //再次申明下一定要用popMail.Messages[i].Headers.GetHeader(“Subject”)的形式, //如果原来页面用的是UTF-8编码popMail.Messages[i].Subject得到的将是直接经jmail解码后的乱码 }
试试其它关键字
接收邮件
发送邮件
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3