最近因为不是太忙,所以就心血来潮的用Lumisoft写了个Web端的邮件收发系统。在此记录开发历程。一、首先先介绍下这个系统的基本思路:把邮件从服务器下载下来然后保存到本地,查看邮件的时候再加载邮件信息。这里,都是用XML,来存储邮件的基本信息。二、用到的类Mail_Item类记录邮件的基本信息Person类记录的是邮件用户名及名称Attachment 类记录附件的基本信息Mail_Summary 邮件信息概要类,用于显示邮件列表User  用户信息类,记录用户信息
/// <summary>
/// 邮件的基本信息
/// </summary>
public class Mail_Item
{
/// <summary>
/// The unique mail Identity in Mail Server
/// </summary>
public string Mail_Id { get; set; }
/// <summary>
/// Mail sender
/// </summary>
public Person Mail_From { get; set; }
/// <summary>
/// Mail Receiver
/// </summary>
public List<Person> Mail_To = new List<Person>();
/// <summary>
/// Mail copy to
/// </summary>
public List<Person> Mail_CopyTo = new List<Person>();
public string Mail_Subject { get; set; }
/// <summary>
/// Mail content path in  localhost
/// </summary>
public string Mail_Body { get; set; }
/// <summary>
/// Mail receive date
/// </summary>
public string Mail_Date { get; set; }
/// <summary>
/// Mail attachments
/// </summary>
public List<Attachment> Mail_Attachment = new List<Attachment>();
/// <summary>
/// mail is read or unread
/// </summary>
public bool IsRead { get; set; }
public bool IsDeleted { get; set; }
}

三、接收邮件
主要步骤有:登录服务器–>判断当前邮件在系统中是否已经存在了->下载邮件,并且保存邮件的基本信息
接收邮件代码:

public static List<Mail_Item> ReceiveMail(User user, out int mailNum)
{
string baseBodyPath =  @"Mails\" + user.Server + @"\" + user.Identity + @"\Mails\";
string baseAttPath =   @"Mails\" + user.Server + @"\" + user.Identity + @"\Attachments\";
CreateFoder(user);
mailNum = 0;
string _mailServer = user.Server;
string _isHelpAccount = user.Account;
string _isHelpPassword = user.Password;
List<Mail_Item> mails = new List<Mail_Item>();
if (string.IsNullOrEmpty(_mailServer) || string.IsNullOrEmpty(_isHelpAccount) || string.IsNullOrEmpty(_isHelpPassword))
{
return mails;
}
string _mailId = string.Empty;
string _subject = string.Empty;  //主题
string _body = string.Empty;    //正文
string _receiveDate = string.Empty;//接收时间
//  Int64 maxUID = GetXmkUserInfo(_mailServer, _isHelpAccount, _isHelpPassword).MaxMailID; //获取上一次接收邮件最大的ID
//if (maxUID < 0)
//{//    return mails;
//}
using (POP3_Client pop = new POP3_Client())
{
try
{
pop.Connect(_mailServer, 110, false);
pop.Login(_isHelpAccount, _isHelpPassword);
POP3_ClientMessageCollection messages = pop.Messages;
//1415170251.6684.Qmail,S=866235:第一部分是递增的
//最后一份邮件都不是最新的,要提示
// _mailId = messages[messages.Count - 1].UID;
for (int i = messages.Count - 1; 0 <= i; i--)
{_mailId = messages[i].UID;
//try
//{//    byte[] bt = messages[i].HeaderToByte();
//    Mail_Message mime_header = Mail_Message.ParseFromByte(bt);
//    Mail_h_Received[] r = mime_header.Received;
//catch
//{ }
//if (Convert.ToInt64(_mailId.Split('.')[0]) <= maxUID)
//    return mails;
try
{
if (CheckMailExists(user, _mailId))  //如果存在
{
continue;
}
}
catch
{
;
}
#region 读取邮件POP3_ClientMessage ms = messages[i];
if (ms != null)
{
List<Person> p_to = new List<Person>(); //收件人列表
List<Person> p_copyTo = new List<Person>();//抄送人列表
Person p_from = new Person();//发送人
List<Attachment> list_att = new List<Attachment>();//附件信息
#region 基本信息
//有新邮件就+1
Mail_Message mime_message = new Mail_Message();
try
{
byte[] messageBytes = ms.MessageToByte();
mime_message = Mail_Message.ParseFromByte(messageBytes);
}
catch
{
continue;
}
mailNum++;
try
{
_subject = mime_message.Subject.Trim() == string.Empty ? "No Subject" : mime_message.Subject;
}
catch (Exception ex)
{
subject = "No Subject";
}
p_from.Address = mime_message.From == null ? "sender is null" : mime_message.From[0].Address;
try
{
string name = "";
if (string.IsNullOrEmpty(mime_message.From[0].DisplayName))
{
int index = mime_message.From[0].Address.IndexOf("@");
name = mime_message.From[0].Address.Substring(0, index);
}
else
{
name = mime_message.From[0].DisplayName;
}
p_from.Name = name;
}
catch (Exception fe)
{
p_from.Name = "SomeOne";
}
try
{
Mail_t_AddressList to = mime_message.To;
if (to.Count > 0 && to != null)
{
for (int ii = 0; ii < to.Mailboxes.Length; ii++)
{
string t_name = "";
if (string.IsNullOrEmpty(to.Mailboxes[ii].DisplayName))
{
int index = to.Mailboxes[ii].Address.IndexOf("@");
t_name = to.Mailboxes[ii].Address.Substring(0, index);
}
else
{
t_name = to.Mailboxes[ii].DisplayName;
}
Person per = new Person
{
Address = to.Mailboxes[ii].Address,
Name = t_name
};
p_to.Add(per);
}
}
}
catch (Exception e)
{
;  try
{
Mail_t_AddressList to = mime_message.To;
if (to.Count > 0 && to != null)
{
for (int ii = 0; ii < to.Mailboxes.Length; ii++)
{
string t_name = "";
if (string.IsNullOrEmpty(to.Mailboxes[ii].DisplayName))
{
int index = to.Mailboxes[ii].Address.IndexOf("@");
t_name = to.Mailboxes[ii].Address.Substring(0, index);
}
else
{
t_name = to.Mailboxes[ii].DisplayName;
}
Person per = new Person
{
Address = to.Mailboxes[ii].Address,
Name = t_name
};
p_to.Add(per);
}
}
}
try
{
Mail_t_AddressList copyTo = mime_message.Cc; //获取的抄送人信息
if (copyTo.Count > 0 && copyTo != null)
{
for (int ii = 0; ii < copyTo.Mailboxes.Length; ii++)
{
string t_name = "";
if (string.IsNullOrEmpty(copyTo.Mailboxes[ii].DisplayName))
{
int index = copyTo.Mailboxes[ii].Address.IndexOf("@");
t_name = copyTo.Mailboxes[ii].Address.Substring(0, index);
}
else
{
t_name = copyTo.Mailboxes[ii].DisplayName;
}
Person per1 = new Person
{
Address = copyTo.Mailboxes[ii].Address,
Name = t_name
};
p_copyTo.Add(per1);
}
}
}
catch (Exception e)
{
;
}
try
{
_receiveDate = mime_message.Date.ToString("yyyy-MM-dd HH:mm:ss");
}
catch
{
Mail_h_Received[] r = mime_message.Received;
_receiveDate = r[0].Time.ToString("yyyy-MM-dd HH:mm:ss") ;
}_body = mime_message.BodyText;try
{
if (!string.IsNullOrEmpty(mime_message.BodyHtmlText))
{
_body = mime_message.BodyHtmlText;
}
}
catch
{
_body = mime_message.BodyText; ;//Response.Write("<script>alert('HTMLBODY');</script>");//屏蔽编码出现错误的问题,错误在BodyText存在而BodyHtmlText不存在的时候,访问BodyHtmlText会出现
}
#endregion
#region 附件信息
MIME_Entity[] attachments = mime_message.GetAttachments(true, true);
foreach (MIME_Entity entity in attachments)
{
Attachment m_att = new Attachment();
string cid;
int eIndex;
if (entity.ContentDisposition != null) //不是内嵌附件
{
cid = entity.ContentID;
string _aName = entity.ContentDisposition.Param_FileName;//区别物理名和逻辑
m_att.A_ID = System.Guid.NewGuid().ToString();
if (string.IsNullOrEmpty(_aName))
{
// _aName = entity.ContentDisposition.Parameters.Owner.ValueToString();
_aName = entity.ContentDescription.ToString();
}
m_att.Name = _aName;
//_attName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + m_att.A_ID;
string type = entity.ContentType.ValueToString();if (!string.IsNullOrEmpty(m_att.Name))
{
string path = Path.Combine(baseAttPath, m_att.A_ID + "_" + m_att.Name);
m_att.Path = path;
try
{
MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body;
Stream decodedDataStream = byteObj.GetDataStream();
using (FileStream fs = new FileStream(serverPath+ path, FileMode.Create))
{try{LumiSoft.Net.Net_Utils.StreamCopy(decodedDataStream, fs, 4000);}catch (Exception e){;}
}
}
catch (Exception e)
{
;
}}
m_att.IsInline = entity.ContentDisposition.DispositionType == MIME_DispositionTypes.Inline;
}
else   //内嵌附件
{
cid = entity.ContentID;
m_att.A_ID = System.Guid.NewGuid().ToString();
m_att.Name = entity.ContentType.Parameters["name"];
try
{
eIndex = m_att.Name.LastIndexOf(".");//有些图片没有后缀名,直接添加一个后缀
if (eIndex == -1)
m_att.Name += ".png";
}
catch (Exception e)
{
m_att.Name = System.Guid.NewGuid().ToString() + ".png";
}
string path = Path.Combine(baseAttPath, m_att.A_ID + "_" + m_att.Name);
m_att.Path = path;string enString = entity.ToString();
int a = enString.LastIndexOf(">");
string base64string = enString.Substring(a + 5);
byte[] bt = Convert.FromBase64String(base64string);
File.WriteAllBytes(serverPath+m_att.Path, bt);
m_att.IsInline = true;
}
m_att.ContentID = entity.ContentID;
try
{
if (cid != null || cid.Trim() != string.Empty)
_body = _body.Replace("cid:" + cid.Substring(1, cid.Length - 2), m_att.Path);}
catch (Exception e)
{
;
}
list_att.Add(m_att);
}
#endregion
//Body内容存储为一个文件
string _bodyFileName = System.Guid.NewGuid().ToString() + ".html";
try
{
File.WriteAllText(serverPath+ baseBodyPath + _bodyFileName, _body,Encoding.GetEncoding("utf-8"));
// File.w
}
catch
{
;
}
Mail_Item mi = new Mail_Item
{
Mail_Id = _mailId,
Mail_From = p_from,
Mail_To = p_to,
Mail_CopyTo = p_copyTo,
Mail_Subject = _subject,
Mail_Body = baseBodyPath + _bodyFileName,
Mail_Attachment = list_att,
Mail_Date = _receiveDate,
IsRead = false,
IsDeleted = false
};
mails.Add(mi);
#endregion
}
}
}
catch (Exception e)
{
;
}
pop.Disconnect();
return mails;
}
}

在接收邮件中不得不提的是邮件内嵌图片的接收,内嵌图片不像普通附件那样,它的entity.ContentDisposition=null,所以得另行讨论,见代码。还有一个关于附件的问题是我用hotmail给我的qq邮箱发的附件,它的entity.ContentDisposition.Param_FileName为null,所以我做了如下处理

string _aName = entity.ContentDisposition.Param_FileName;//区别物理名和逻辑
m_att.A_ID = System.Guid.NewGuid().ToString();
if (string.IsNullOrEmpty(_aName))
{
// _aName = entity.ContentDisposition.Parameters.Owner.ValueToString();
_aName = entity.ContentDescription.ToString();
}

自此基本的邮件接收就没问题了。
以下是我写的邮件系统截图:


项目地址Web 邮件系统

C#.NET实现基于Lumisoft的邮件收发功能相关推荐

  1. 基于Javamail的邮件收发系统(系统+论文+开题报告+任务书+外文翻译+文献综述+答辩PPT)

    毕业设计(论文) ( 20 届) 论文(设计)题目 基于Javamail的邮件收发系统 作 者 二级学院.专业 班 级 指导教师(职称) 论 文 字 数 论文完成时间 20年月日 基于JavaMail ...

  2. 基于JavaMail的邮件收发系统

    基于JavaMail的邮件收发系统 摘 要 电子邮件在当今社会中扮演了一个很重要的角色.越来越多的人在使用它.而且用它的人数势必会继续增加.本文介绍了Javamail邮件收发系统的开发背景,对国内外现 ...

  3. java pop邮件 源码_[源码和文档分享]基于JavaMail的邮件收发系统

    摘 要 电子邮件在当今社会中扮演了一个很重要的角色.越来越多的人在使用它.而且用它的人数势必会继续增加.本文介绍了Javamail邮件收发系统的开发背景,对国内外现有的多种成熟的电子邮件系统进行分析和 ...

  4. think php ajax分页,thinkPHP5框架实现基于ajax的分页功能示例

    本文实例讲述了thinkPHP5框架实现基于ajax的分页功能.分享给大家供大家参考,具体如下: 最近一个页面的选项卡又牵扯到ajax分页,所以研究了一下tp5的ajax分页使用方法 首先看一下tp5 ...

  5. 基于nginx实现缓存功能及uptream模块详细使用方法

    基于nginx实现缓存功能及uptream模块详细使用方法 一般情况下,前端使用nginx做代理或7层负载并向后实现varish/squid做cache server的效果要好的多 nginx与squ ...

  6. java 调用foxmail_java邮件收发功能实现代码

    本文实例为大家分享了邮件收发功能的具体实现代码,供大家参考,具体内容如下 准备工作, 环境搭建: 1. 本地搭建一个邮件服务器  易邮服务器,eyoumailserversetup.exe 2. 新建 ...

  7. 基于AVR单片机PWM功能的数控恒流源研制

    随着电子技术的深入发展,各种智能仪器越来越多,涉及领域越来越广,而仪器对电源的要求也越来越高.现今,电源设备有朝着数字化方向发展的趋势.然而绝大多数数控电源设计是通过高位数的A/D和D/A芯片来实现的 ...

  8. javamail 解码 base64 html格式邮件_[源码和文档分享]基于JavaMail的邮件收发系统

    摘 要 电子邮件在当今社会中扮演了一个很重要的角色.越来越多的人在使用它.而且用它的人数势必会继续增加.本文介绍了Javamail邮件收发系统的开发背景,对国内外现有的多种成熟的电子邮件系统进行分析和 ...

  9. 基于STM32的多功能门禁系统(AS608指纹识别、密码解锁、刷卡解锁)

    目录 一.项目功能 二.视频 三.原理图 4.材料选择 5.部分程序 资料下载地址:基于STM32的多功能门禁系统 一.项目功能 1.AS608指纹解锁:可以录入.删除.验证指纹: 2.密码解锁:可以 ...

最新文章

  1. Vue的自定义滚动,我用el-scrollbar
  2. Python3 AttributeError: module 'cv2' has no attribute 'SIFT'
  3. 几种常见数据库连接池的使用比较
  4. 【PyTorch 】interpolate()==>上下采样函数
  5. 穿越剧_零差评的5部穿越剧,少有的巅峰之作,第一堪称穿越鼻祖!
  6. EmacsLisp学习
  7. javascript小实例,阻止浏览器默认行为,真的能阻止吗?支持IE和标准浏览器的阻止默认行为的方法...
  8. sql语句分析是否走索引_Mysql中SQL语句不使用索引的情况
  9. MAC iterm2配置rz sz
  10. 在四位共阴极数码上显示“2 3 5 8”四个数字
  11. 面向对象练习:快递柜代码
  12. vue渲染大量数据优化_Vue列表页渲染优化详解
  13. 99%的程序员都不明白:弱者和强者的唯一区别
  14. 苹果 watchOS 3.2 首个测试版:剧场模式、SiriKit
  15. 教师备课计算机教师管理制度,计算机学院教学过程管理中教师职责与问责暂行规定--中地大计字[2016]03号...
  16. 【PS】平面设计:如何旋转图片
  17. nginx cache 总结
  18. 我们该如何高效的学习?
  19. 2019尚硅谷大数据Javaweb篇三 Ajax、JSTL、会话技术、过滤器、监听器、xml、json
  20. DICOM:fo-dicom、dcm4che14、dcm4chee等开源库持续自我维护

热门文章

  1. java获取枚举索引_Java枚举使用详解
  2. 无线蓝牙耳机什么牌子好一点?2022年蓝牙耳机推荐
  3. HUAWEI PAP认证
  4. 在Windows系统上使用WSL和Docker
  5. firefox常见问题解答
  6. quill Cannot import ImageResize. Are you sure it was registered?
  7. JavaScript基础知识笔记
  8. 深圳python如何评价_如何评价shen 语言?
  9. 经典智力题:经理年龄问题
  10. 商用室内机器人才是未来机器人开发的热点与趋势