OpenPop的Windows服务从GetMessage代码退出,相同的代码OK win app

时间:2013-04-10 13:20:23

标签: c# windows-services openpop gmail-pop

我们正在实施使用

下载Gmail A.C附件的代码

openpop3命名空间。在此代码中,我们正在检查附件大小(如果附件)

size大于指定值(在kb中配置文件中设置的值)。然后它必须

向发件人发送电子邮件....                 它在Windows应用程序中工作正常,但每当我实现代码

在Window服务中遇到问题。它从这行代码退出函数

OpenPop.Mime.Message m = popClient.GetMessage(i);

框架:3.5 V.S:2008 语言# 打开POP命名空间V2.0.4.369

This is my code

 OpenPop.Mime.Message m = popClient.GetMessage(i);


private void ReceiveMails()
    {

            Utility.Log = true;
            if (popClient.Connected)
            {
                popClient.Disconnect();
            }
            popClient.Connect(POPServer, port, ssl);
            popClient.Authenticate(username, password);
            int Count = popClient.GetMessageCount();
            writeToLogFile("Total Mail count is:" + Count.ToString());
            if (Count > 0)
            {
                for (int i = 1; i <= Count; i++)
                {
                    flag = false;

                    OpenPop.Mime.Message m = popClient.GetMessage(i);
                    Sub = m.Headers.Subject;
                   int size = popClient.GetMessageSize(i);
                    int mailsize = int.Parse(ConfigurationSettings.AppSettings

["emailSize"]) * 1024;



                    if (size < mailsize)
                    {
                    //we are checking the sub of Email
                    for (int j = 1; j < 30; j++)
                    {

                        strFranchisekey = ConfigurationSettings.AppSettings

["Franchise" + j];
                        if (strFranchisekey != "")
                        {
                            int inex = strFranchisekey.IndexOf("=");
                            strFranchiseshortvalue = strFranchisekey.Substring

(0, inex);

                            if (Sub.Contains(strFranchiseshortvalue))
                            {
                                flag = true;

                                foreach (OpenPop.Mime.MessagePart attachment in 

m.FindAllAttachments())
                                {
                                    writeToLogFile(attachment.FileName);
                                    file = attachment.FileName;
                                    index = strFranchisekey.IndexOf("=");
                                    string StrCity = strFranchisekey.Substring

(index + 1);
                                    strFolderPath = 

(ConfigurationSettings.AppSettings["FolderPath" +StrCity]);
                                    StrSubFolderPath = 

(ConfigurationSettings.AppSettings["SubPath" + StrCity]);
                                    if (Directory.Exists(strFolderPath))
                                    //we are checking folder exists or not ?
                                    {
                                        File.WriteAllBytes(strFolderPath + "\\" 

+ file, attachment.Body);
                                    }
                                    //
                                    else if (Directory.Exists

(StrSubFolderPath))
                                    {
                                        File.WriteAllBytes(StrSubFolderPath + 

"\\" + file, attachment.Body);
                                    }
                                    else
                                    {
                                        //we can give here invalid path.
                                        File.WriteAllBytes

(ConfigurationSettings.AppSettings["InvalidPath"] + "\\" + file, attachment.Body);
                                        sendEmail(i);

                                    }
                                }
                                break;
                            }

                        }
                    }
                    if (flag != true)
                    {
                        writeToLogFile("matching franchise name is not found");

                        foreach (OpenPop.Mime.MessagePart attachment in 

m.FindAllAttachments())
                        {
                            File.WriteAllBytes

(ConfigurationSettings.AppSettings["InvalidPath"] + "\\" + file, attachment.Body);

                        }
                        sendEmail(i);
                    }

                }
             }
    else
  {
     writeToLogFile("Please reduce the  email size");
   }

            }
            else
            {
                writeToLogFile("No New Attachment");
            }



        }

1 个答案:

答案 0 :(得分:0)

谢谢@Antonio Bakula我在我的Windows服务应用程序中写了try catch块并记录它。然后我理解了我的bug并且它给了异常,无法读取消息,另一个实例已经在阅读。这是因为代码是基于计时器的

我每隔1分钟开火一次。现在我添加了代码,一旦它启动电子邮件处理就停止计时器,并在完成电子邮件处理代码后启动计时器。