1 public ExchangeService GetServices() 2 { 3 System.Net.ServicePointManager.ServerCertificateValidationCallback 4 = delegate( 5 Object obj, 6 X509Certificate certificate, 7 X509Chain chain, 8 SslPolicyErrors errors 9 ) 10 { 11 return true; 12 // Validate the certificate and return true or false as appropriate. // Note that it not a good practice to always return true because not // all certificates should be trusted. 13 }; 14 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010); 15 16 18
string[] cfg = Config.UserName.Split('\\'); 19 service.Credentials = new WebCredentials(cfg[1], Config.Password, cfg[0]);
26 service.Url = new Uri(Config.Server); 27 28 return service; 29 }
这样的代码会报错吗?
你得把你的异常也贴出来,不然没人知道你哪儿错了
就是放在正式环境上然后就显示报错到这里 从日志上面看的
12/13/2018 12:21:01 PM:
EmailService:获取邮件失败: at TonkNet.BDA.Core.Service.Common.ExchangeEmailReciver.GetMailMessage()
at SendEmail.ReciveMessagesService.LoadNeadSendMessages()
---------------------------------------------
这是日志信息 GetMailMessage 写了try catch catch只有上面的代码没有写到try里面 估摸着就是这报的错
@Raceme: 多贴一点信息,信息太少,很少有人能看懂
@老六代码:
List<EmailMessage> list = new List<EmailMessage>();
Hashtable saos = new Hashtable();
try
{
if (this.pop3Cofig == null)
{
pop3Cofig = LoadPop3Config();
}
EmailReciver reviver = null;
//string IsExchange = System.Configuration.ConfigurationSettings.AppSettings["aaaaa"].ToString();
string IsExchange = System.Configuration.ConfigurationSettings.AppSettings["IsExchange"].ToString();
if (IsExchange=="true")
{
reviver = new ExchangeEmailReciver(Conf.appName, pop3Cofig);
}
else
{
reviver = new Pop3EmailReciver(Conf.appName, pop3Cofig);
}
reviver.ReciveEndCallback += new EmailReciver.ReciveEnd(ReciveEndCallback);
list = reviver.GetMailMessage();
}
catch (Exception error)
{
Log("获取邮件失败:" + error.StackTrace);
Log(string.Format("老时间:{0},新时间{1}", SendTime.ToString(),DateTime.Now.ToString()));
TimeSpan ts = DateTime.Now - SendTime;
if (ts.TotalMinutes > 30)
{
SendTime=DateTime.Now;
//发送邮件给固定人提醒服务错误
string MailTo = Conf.monitorMail;
string MailFrom = Conf.mailFrom;
string Title = "邮箱服务出现异常,请查看详细信息并联系管理员";
string Opt = "HRMLEmailReciveService";
SelectData("Proc_Sys_CreateEmailForOAApp", new string[] { "@Sender", "@SendFromEmail", "@SendTo", "@SendToEmail", "@EmailTitle", "@EmailContext", "@Opt" }, new object[] { MailFrom, MailFrom, MailTo, MailTo, Title, error.StackTrace, Opt });
}
}
return list;
日志报错信息
at TonkNet.BDA.Core.Service.Common.ExchangeEmailReciver.GetMailMessage()
at SendEmail.ReciveMessagesService.LoadNeadSendMessages()
ExchangeEmailReciver.GetMailMessage方法如下:
public override List<EmailMessage> GetMailMessage()
{
var list = new List<EmailMessage>();
var service = GetServices();
try
{
//创建过滤器, 条件为邮件未读.
SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
FindItemsResults<Item> findResults = service.FindItems(
WellKnownFolderName.Inbox,
new ItemView(100));
int messageCount = findResults.TotalCount; // client.GetMessageCount();
IList<ItemId> deleteIndexes = new List<ItemId>();
DateTime lastUpdateTime = DateTime.Now;
int index = Config.LastReciveIndex < 1 ? 1 : Config.LastReciveIndex;
if (findResults != null)
{
foreach (Item item in findResults.Items)
{
Config.LastReciveIndex = index + 1;
Microsoft.Exchange.WebServices.Data.EmailMessage email =
Microsoft.Exchange.WebServices.Data.EmailMessage.Bind(service, item.Id);
lastUpdateTime = email.LastModifiedTime;
if (lastUpdateTime.CompareTo(Config.LastReciveTime) > 0)
{
if (Config.DeleteRemoteEmail)
{
deleteIndexes.Add(item.Id);
}
var subject = email.Subject;
var body = email.Body;
var receivedDate = email.DateTimeReceived;
var em = new TonkNet.BDA.Core.Kernel.Message.EmailMessage(email.From.Address, subject,
body.Text, receivedDate);
em.MessageID = item.ConversationId.UniqueId;
list.Add(em);
}
////标记为已读
//email.IsRead = true;
////将对邮件的改动提交到服务器
//email.Update(ConflictResolutionMode.AlwaysOverwrite);
//email.Delete(DeleteMode.MoveToDeletedItems);
}
}
if (Config.DeleteRemoteEmail)
{
if (deleteIndexes.Count != 0)
{
service.DeleteItems(deleteIndexes, DeleteMode.MoveToDeletedItems, null, null);
}
Config.LastReciveIndex = 1;
}
if (ReciveEndCallback != null)
{
ReciveEndCallback(this.AppName, lastUpdateTime, Config.LastReciveIndex, list);
}
}
catch (Exception e)
{
}
return list;
}
写了try catch 如何是try里面的报错,应该会返回list吧?
所以我估摸着是GetServices()报错了
GetServices()方法如下:
public ExchangeService GetServices()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback
= delegate(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
// Validate the certificate and return true or false as appropriate. // Note that it not a good practice to always return true because not // all certificates should be trusted.
};
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
// Setting credentials is unnecessary when you connect from a computer that is
// logged on to the domain.
string[] cfg = Config.UserName.Split('\\');
service.Credentials = new WebCredentials(cfg[1], Config.Password, cfg[0]);
service.Url = new Uri(Config.Server);
return service;
}