4 个解决方案
#1
这个问题报错是socket的报错,我跟踪了它的源码,也没看出什么破绽,有了解仁兄还请不吝赐教
#2
旧版本的无论webform还是winform都不会在连接服务器的时候出现socket报错(报错代码10014),但是实现收取imap邮件代码过长,非常不好用,所以才决定有新版本的lumisoft
#3
我的源码如下:
#region 同步POP3邮件服务器
/// <summary>
/// 同步本地邮件与pop3服务器邮件
/// </summary>
/// <param name="server"></param>
/// <param name="port"></param>
/// <param name="isSSL"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <param name="zhbh"></param>
/// <returns></returns>
public bool GetPopMailFromServer(string server, int port, bool isSSL, string username, string pwd)
{
bool result = false;
try
{
POP3_Client pop = new POP3_Client();
pop.Connect(server, port, isSSL);
pop.Authenticate(username, pwd, true);
if (pop.IsAuthenticated)
{
IList<YJ> mail_list = new List<YJ>();
IList<MFJ> attach_list = new List<MFJ>();
IList<string> uid_list = dal.QueryUID();
POP3_ClientMessageCollection cmc = pop.Messages;
foreach (POP3_ClientMessage cm in cmc)
{
if (!uid_list.Contains(cm.UID))
{
byte[] bt = cm.MessageToByte();
sbyte[] sbt = new sbyte[bt.Length];
Buffer.BlockCopy(bt, 0, sbt, 0, bt.Length);
string encodeName = IdentifyEncoding.GetEncodingName(sbt).ToLower();//解析编码方式 避免乱码
Mail_Message msg = Mail_Message.ParseFromByte(bt, Encoding.GetEncoding(encodeName));
//Mail_Message msg = Mail_Message.ParseFromByte(bt);
YJ mail = new YJ();
mail.FJR = msg.From.ToString();
mail.ZT = msg.Subject.ToString();
mail.SCSJ = msg.Date;
mail.SJR = msg.To.ToString();
mail.ZW = msg.BodyHtmlText;
mail.MUID = cm.UID;
mail.BH = Guid.NewGuid().ToString();
if (msg.Attachments.Length > 0)
{
foreach (MIME_Entity me in msg.Attachments)
{
MFJ attach = new MFJ();
//byte[] attachByte = me.ToByte(null, null);
//attach.FJ = attachByte;
string attachpath = System.AppDomain.CurrentDomain.BaseDirectory + "Attachment\\";//路径
string attachname = me.ContentDisposition.Param_FileName;//文件名 含后缀
string totalname = attachpath + attachname;//完全路径
FileInfo fi = new FileInfo(totalname);
if (fi.Exists)
{
string prefix = attachname.Substring(0, attachname.LastIndexOf("."));
string postfix = attachname.Replace(prefix, "");
attachname = prefix + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + postfix;
totalname = attachpath + attachname;
}
File.WriteAllBytes(totalname, ((MIME_b_SinglepartBase)me.Body).Data);
attach.FJMC = attachname;
attach.BH = Guid.NewGuid().ToString();
attach.YJBH = mail.BH;
attach_list.Add(attach);
}
}
mail_list.Add(mail);
}
}
result = dal.InsMail(mail_list, attach_list);
}
pop.Disconnect();
pop.Dispose();
}
catch (POP3_ClientException)
{
result = false;
throw;
}
return result;
}
#endregion
#region 同步POP3邮件服务器
/// <summary>
/// 同步本地邮件与pop3服务器邮件
/// </summary>
/// <param name="server"></param>
/// <param name="port"></param>
/// <param name="isSSL"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <param name="zhbh"></param>
/// <returns></returns>
public bool GetPopMailFromServer(string server, int port, bool isSSL, string username, string pwd)
{
bool result = false;
try
{
POP3_Client pop = new POP3_Client();
pop.Connect(server, port, isSSL);
pop.Authenticate(username, pwd, true);
if (pop.IsAuthenticated)
{
IList<YJ> mail_list = new List<YJ>();
IList<MFJ> attach_list = new List<MFJ>();
IList<string> uid_list = dal.QueryUID();
POP3_ClientMessageCollection cmc = pop.Messages;
foreach (POP3_ClientMessage cm in cmc)
{
if (!uid_list.Contains(cm.UID))
{
byte[] bt = cm.MessageToByte();
sbyte[] sbt = new sbyte[bt.Length];
Buffer.BlockCopy(bt, 0, sbt, 0, bt.Length);
string encodeName = IdentifyEncoding.GetEncodingName(sbt).ToLower();//解析编码方式 避免乱码
Mail_Message msg = Mail_Message.ParseFromByte(bt, Encoding.GetEncoding(encodeName));
//Mail_Message msg = Mail_Message.ParseFromByte(bt);
YJ mail = new YJ();
mail.FJR = msg.From.ToString();
mail.ZT = msg.Subject.ToString();
mail.SCSJ = msg.Date;
mail.SJR = msg.To.ToString();
mail.ZW = msg.BodyHtmlText;
mail.MUID = cm.UID;
mail.BH = Guid.NewGuid().ToString();
if (msg.Attachments.Length > 0)
{
foreach (MIME_Entity me in msg.Attachments)
{
MFJ attach = new MFJ();
//byte[] attachByte = me.ToByte(null, null);
//attach.FJ = attachByte;
string attachpath = System.AppDomain.CurrentDomain.BaseDirectory + "Attachment\\";//路径
string attachname = me.ContentDisposition.Param_FileName;//文件名 含后缀
string totalname = attachpath + attachname;//完全路径
FileInfo fi = new FileInfo(totalname);
if (fi.Exists)
{
string prefix = attachname.Substring(0, attachname.LastIndexOf("."));
string postfix = attachname.Replace(prefix, "");
attachname = prefix + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + postfix;
totalname = attachpath + attachname;
}
File.WriteAllBytes(totalname, ((MIME_b_SinglepartBase)me.Body).Data);
attach.FJMC = attachname;
attach.BH = Guid.NewGuid().ToString();
attach.YJBH = mail.BH;
attach_list.Add(attach);
}
}
mail_list.Add(mail);
}
}
result = dal.InsMail(mail_list, attach_list);
}
pop.Disconnect();
pop.Dispose();
}
catch (POP3_ClientException)
{
result = false;
throw;
}
return result;
}
#endregion
#4
我刚开始看lumisoft.net。不会,帮顶。
#1
这个问题报错是socket的报错,我跟踪了它的源码,也没看出什么破绽,有了解仁兄还请不吝赐教
#2
旧版本的无论webform还是winform都不会在连接服务器的时候出现socket报错(报错代码10014),但是实现收取imap邮件代码过长,非常不好用,所以才决定有新版本的lumisoft
#3
我的源码如下:
#region 同步POP3邮件服务器
/// <summary>
/// 同步本地邮件与pop3服务器邮件
/// </summary>
/// <param name="server"></param>
/// <param name="port"></param>
/// <param name="isSSL"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <param name="zhbh"></param>
/// <returns></returns>
public bool GetPopMailFromServer(string server, int port, bool isSSL, string username, string pwd)
{
bool result = false;
try
{
POP3_Client pop = new POP3_Client();
pop.Connect(server, port, isSSL);
pop.Authenticate(username, pwd, true);
if (pop.IsAuthenticated)
{
IList<YJ> mail_list = new List<YJ>();
IList<MFJ> attach_list = new List<MFJ>();
IList<string> uid_list = dal.QueryUID();
POP3_ClientMessageCollection cmc = pop.Messages;
foreach (POP3_ClientMessage cm in cmc)
{
if (!uid_list.Contains(cm.UID))
{
byte[] bt = cm.MessageToByte();
sbyte[] sbt = new sbyte[bt.Length];
Buffer.BlockCopy(bt, 0, sbt, 0, bt.Length);
string encodeName = IdentifyEncoding.GetEncodingName(sbt).ToLower();//解析编码方式 避免乱码
Mail_Message msg = Mail_Message.ParseFromByte(bt, Encoding.GetEncoding(encodeName));
//Mail_Message msg = Mail_Message.ParseFromByte(bt);
YJ mail = new YJ();
mail.FJR = msg.From.ToString();
mail.ZT = msg.Subject.ToString();
mail.SCSJ = msg.Date;
mail.SJR = msg.To.ToString();
mail.ZW = msg.BodyHtmlText;
mail.MUID = cm.UID;
mail.BH = Guid.NewGuid().ToString();
if (msg.Attachments.Length > 0)
{
foreach (MIME_Entity me in msg.Attachments)
{
MFJ attach = new MFJ();
//byte[] attachByte = me.ToByte(null, null);
//attach.FJ = attachByte;
string attachpath = System.AppDomain.CurrentDomain.BaseDirectory + "Attachment\\";//路径
string attachname = me.ContentDisposition.Param_FileName;//文件名 含后缀
string totalname = attachpath + attachname;//完全路径
FileInfo fi = new FileInfo(totalname);
if (fi.Exists)
{
string prefix = attachname.Substring(0, attachname.LastIndexOf("."));
string postfix = attachname.Replace(prefix, "");
attachname = prefix + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + postfix;
totalname = attachpath + attachname;
}
File.WriteAllBytes(totalname, ((MIME_b_SinglepartBase)me.Body).Data);
attach.FJMC = attachname;
attach.BH = Guid.NewGuid().ToString();
attach.YJBH = mail.BH;
attach_list.Add(attach);
}
}
mail_list.Add(mail);
}
}
result = dal.InsMail(mail_list, attach_list);
}
pop.Disconnect();
pop.Dispose();
}
catch (POP3_ClientException)
{
result = false;
throw;
}
return result;
}
#endregion
#region 同步POP3邮件服务器
/// <summary>
/// 同步本地邮件与pop3服务器邮件
/// </summary>
/// <param name="server"></param>
/// <param name="port"></param>
/// <param name="isSSL"></param>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <param name="zhbh"></param>
/// <returns></returns>
public bool GetPopMailFromServer(string server, int port, bool isSSL, string username, string pwd)
{
bool result = false;
try
{
POP3_Client pop = new POP3_Client();
pop.Connect(server, port, isSSL);
pop.Authenticate(username, pwd, true);
if (pop.IsAuthenticated)
{
IList<YJ> mail_list = new List<YJ>();
IList<MFJ> attach_list = new List<MFJ>();
IList<string> uid_list = dal.QueryUID();
POP3_ClientMessageCollection cmc = pop.Messages;
foreach (POP3_ClientMessage cm in cmc)
{
if (!uid_list.Contains(cm.UID))
{
byte[] bt = cm.MessageToByte();
sbyte[] sbt = new sbyte[bt.Length];
Buffer.BlockCopy(bt, 0, sbt, 0, bt.Length);
string encodeName = IdentifyEncoding.GetEncodingName(sbt).ToLower();//解析编码方式 避免乱码
Mail_Message msg = Mail_Message.ParseFromByte(bt, Encoding.GetEncoding(encodeName));
//Mail_Message msg = Mail_Message.ParseFromByte(bt);
YJ mail = new YJ();
mail.FJR = msg.From.ToString();
mail.ZT = msg.Subject.ToString();
mail.SCSJ = msg.Date;
mail.SJR = msg.To.ToString();
mail.ZW = msg.BodyHtmlText;
mail.MUID = cm.UID;
mail.BH = Guid.NewGuid().ToString();
if (msg.Attachments.Length > 0)
{
foreach (MIME_Entity me in msg.Attachments)
{
MFJ attach = new MFJ();
//byte[] attachByte = me.ToByte(null, null);
//attach.FJ = attachByte;
string attachpath = System.AppDomain.CurrentDomain.BaseDirectory + "Attachment\\";//路径
string attachname = me.ContentDisposition.Param_FileName;//文件名 含后缀
string totalname = attachpath + attachname;//完全路径
FileInfo fi = new FileInfo(totalname);
if (fi.Exists)
{
string prefix = attachname.Substring(0, attachname.LastIndexOf("."));
string postfix = attachname.Replace(prefix, "");
attachname = prefix + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + postfix;
totalname = attachpath + attachname;
}
File.WriteAllBytes(totalname, ((MIME_b_SinglepartBase)me.Body).Data);
attach.FJMC = attachname;
attach.BH = Guid.NewGuid().ToString();
attach.YJBH = mail.BH;
attach_list.Add(attach);
}
}
mail_list.Add(mail);
}
}
result = dal.InsMail(mail_list, attach_list);
}
pop.Disconnect();
pop.Dispose();
}
catch (POP3_ClientException)
{
result = false;
throw;
}
return result;
}
#endregion
#4
我刚开始看lumisoft.net。不会,帮顶。