104 lines
4.0 KiB
C#
104 lines
4.0 KiB
C#
using OpenPop.Mime;
|
||
using OpenPop.Pop3;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text.RegularExpressions;
|
||
using WX.CRM.BLL.Sms;
|
||
using WX.CRM.IBLL.Sms;
|
||
|
||
namespace WX.CRM.CRMServices.Email
|
||
{
|
||
public class ReadEmail
|
||
{
|
||
#region field
|
||
ISMS_FROMEMAIL smsbl = new SMS_FROMEMAIL_BL();
|
||
//mime解析,此服务有待改进!
|
||
string _Host = "pop.qpasset.com"; //主机名称或IP地址
|
||
int _Port = 110; //主机的端口号(默认为110)
|
||
string _username = "xssp@qpasset.com"; //邮箱帐号
|
||
string _pwd = "EDLS05ma"; //邮箱登陆密码
|
||
#endregion
|
||
|
||
public void ExecEmail()
|
||
{
|
||
_Host = WX.CRM.Common.Utility.GetSettingByKey("EmailPop3Ip");
|
||
_Port = int.Parse(WX.CRM.Common.Utility.GetSettingByKey("EmailPop3Port"));
|
||
_username = WX.CRM.Common.Utility.GetSettingByKey("EmailReceived");
|
||
_pwd = WX.CRM.Common.Utility.GetSettingByKey("EmailPwd");
|
||
ExecEmailContent();
|
||
}
|
||
|
||
public void ExecEmailContent()
|
||
{
|
||
try
|
||
{
|
||
var emailLis = FetchAllMessages(_Host, _Port, false, _username, _pwd);
|
||
if (emailLis == null)
|
||
return;
|
||
int j = 0;
|
||
foreach (var obj in emailLis)
|
||
{
|
||
bool is_log = false;
|
||
string cotent = "";
|
||
if (obj.MessagePart.Body != null)
|
||
cotent = obj.MessagePart.BodyEncoding.GetString(obj.MessagePart.Body);
|
||
else
|
||
{
|
||
var objs = obj.MessagePart.MessageParts[0].Body;
|
||
cotent = obj.MessagePart.MessageParts[0].BodyEncoding.GetString(objs);
|
||
}
|
||
//var cotent = Encoding.Default.GetString(objs);
|
||
|
||
WX.CRM.Model.Entity.SMS_FROMEMAIL emailInfo = new WX.CRM.Model.Entity.SMS_FROMEMAIL();
|
||
emailInfo.EMAIL_CONTENT = cotent;
|
||
emailInfo.CTIME = System.DateTime.Now;
|
||
//您的连售模拟盘交易账户是:666600234138,交易密码:222364,请您妥善保管
|
||
Match matchCode = Regex.Match(cotent, @"交易账户是:\d{12}");
|
||
Match matchPwd = Regex.Match(cotent, @"交易密码:\d{6}");
|
||
if (matchCode.Success)
|
||
{
|
||
is_log = true;
|
||
string month1 = matchCode.Groups[0].Value;
|
||
emailInfo.TRADECODE = month1.Split(':')[1];
|
||
}
|
||
if (matchPwd.Success)
|
||
{
|
||
string month2 = matchPwd.Groups[0].Value;
|
||
emailInfo.TRADEPWD = month2.Split(':')[1];
|
||
}
|
||
if (is_log)
|
||
smsbl.Insert(emailInfo);
|
||
|
||
j++;
|
||
if (j >= 50)
|
||
break;
|
||
}
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
WX.CRM.Common.LogHelper.Error(string.Concat("【ReadEmail.ExecEmailContent()】:", err.Message, err.StackTrace));
|
||
}
|
||
}
|
||
List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
|
||
{
|
||
using (Pop3Client client = new Pop3Client())
|
||
{
|
||
client.Connect(hostname, port, useSsl);
|
||
client.Authenticate(username, password);
|
||
int messageCount = client.GetMessageCount();
|
||
List<Message> allMessages = new List<Message>(messageCount);
|
||
int j = 0;
|
||
for (int i = messageCount; i > 0; i--)
|
||
{
|
||
allMessages.Add(client.GetMessage(i));
|
||
j++;
|
||
if (j >= 50)
|
||
break;
|
||
}
|
||
return allMessages;
|
||
}
|
||
}
|
||
//==============================================
|
||
}
|
||
}
|