简体   繁体   English

OpenPop的Windows服务从GetMessage代码退出,相同的代码可以赢得应用

[英]Windows service of OpenPop exit from GetMessage code, same code OK win app

We are implementing the code of downloading the attachment of Gmail AC using 我们正在实施使用以下代码下载Gmail AC附件的代码

openpop3 namespace. openpop3名称空间。 In this code we are checking the attachment size if attachment 在此代码中,我们正在检查附件的大小

size is greater than specify value (value set in config file in kb).then it has to 大小大于指定值(在配置文件中以kb为单位设置的值)。

send email to sender .... It works fine in windows application but whenever I implement code 发送电子邮件给发件人....在Windows应用程序中工作正常,但是每当我实现代码时

in Window service it is getting a problem. 在Windows服务中,它遇到了问题。 It exit function from this line of code 它从这行代码退出函数

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

Framework:3.5 VS:2008 Language# Open POP namespace V2.0.4.369 框架:3.5 VS: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");
            }



        }

Thanks @Antonio Bakula I wrote try catch block in my windows service app and logging it. 感谢@Antonio Bakula,我在Windows服务应用中编写了try catch块并记录了它。 Then I understood my bug and it gave exception, can't read message another instance is already reading. 然后我理解了我的错误,并给出了异常,无法读取消息,另一个实例已在读取。 This was because the code is timer-based and 这是因为代码是基于计时器的,

I was firing event after every 1 minute. 我每1分钟触发一次比赛。 Now I added code to stop timer as soon as it starts email processing and start timer once it finishes email processing code. 现在,我添加了代码以在启动电子邮件处理后立即停止计时器,并在完成电子邮件处理代码后立即启动计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM