使用.net中的Pop3.dll从MailBox下载电子邮件附件

时间:2012-10-15 03:56:20

标签: c# .net winforms pop3 email-attachments

我使用vs 2010开发使用c#的winform应用程序。

对于从邮箱中下载电子邮件附件我使用过pop3.dll

我的代码下载附件当我在我的机器上使用它时直接连接到互联网但是当我在ISA服务器2004用作防火墙的域内使用它时我的代码只显示邮箱有多少邮件和然后去等待

请有人告诉我为什么以及如何解决这个问题。

这是代码

public bool DownloadEmailWithAttachment(string Type)
    {
        try
        {
            EventLog.WriteEntry("Service", "Attachment Path :> " + AttachmentDir);
            long count = 0;
            Pop3Client email = new Pop3Client(UserName, Password, IncomingPopAddress);
            email.OpenInbox();                
            //LogEntry();
            EventLog.WriteEntry("Service", "Counter :> " + email.MessageCount);
            while (email.NextEmail())
            {
                count = email.MessageCount;
                if (email.IsMultipart)
                {

                    IEnumerator enumerator = email.MultipartEnumerator;
                    while (enumerator.MoveNext())
                    {
                        Pop3Component multipart = (Pop3Component)enumerator.Current;
                        string checkIfFile = multipart.Filename;

                        if (checkIfFile != null)
                        {
                            //Console.WriteLine("checkIfFile.Contains('Leave') =" + checkIfFile.ToString());
                            EventLog.WriteEntry("Service", "checkIfFile.Contains('"+Type+"') =" + checkIfFile.ToString());
                            if (checkIfFile.Contains(""+Type))
                            {
                                EventLog.WriteEntry("Service", "Attachment name=" + multipart.Name); // ... etc
                                byte[] filebytes = Encoding.ASCII.GetBytes(multipart.Data);

                                //Search FileName
                                int fileLength = checkIfFile.Length;
                                EventLog.WriteEntry("Service", "fileLength=" + fileLength);
                                //int Begin = multipart.ContentType.IndexOf("name="+checkIfFile.ToString());
                                string leFileNale = checkIfFile.ToString();
                                EventLog.WriteEntry("Service", leFileNale);
                                FileStream LeFS = new FileStream(AttachmentDir + "\\" + leFileNale, FileMode.Create, FileAccess.Write, FileShare.None);
                                LeFS.Write(filebytes, 0, filebytes.Length);
                                LeFS.Close();
                                string DeleteMailContainingSubject = email.Subject.ToString();
                                if (email.Subject.Contains(DeleteMailContainingSubject))
                                {
                                    email.DeleteEmail();
                                    EventLog.WriteEntry("Service", "Email with Subject " + DeleteMailContainingSubject);
                                }
                            }
                        }
                    }
                }
            }                
            email.CloseConnection();
        }
        catch (Pop3LoginException ex)
        {
            EventLog.WriteEntry("Service","Exception :" + ex.ToString());
        }
        return true;
    }

这是我下载电子邮件附件的代码。

0 个答案:

没有答案