2009-10-07 10 views
4

나는 C#에서 sourceforge의 "Koolwired.Imap"이라는 오픈 소스 프로젝트를 사용하여이 작업을 시도했습니다.C#의 IMAP - 메일 및 첨부 파일 다운로드

메일을 다운로드 할 때도 문제가 없지만 첨부 파일의 경우 첨부 파일의 파일 이름 만 나열됩니다. 이것을 시도한 사람이 있습니까?

동일한 기능을 수행 할 수있는 다른 더 좋은 무료 라이브러리 (내 캠퍼스 프로젝트를 위해이 일을하고 있기 때문에 나는이에 대한 무료/오픈 소스 솔루션이 필요합니다)

ImapConnect connection = new ImapConnect("imap.gmail.com", 993, true); 
ImapCommand command = new ImapCommand(connection); 
ImapAuthenticate auth = new ImapAuthenticate(connection, "<username>@gmail.com", "<password>"); 
connection.Open(); 
auth.Login(); 

string htmlbody = ""; 
ImapMailbox mailbox = command.Select("INBOX"); 
mailbox = command.Fetch(mailbox); 
int mailCount = mailbox.Messages.Count; 

for (int i = 0; i < mailCount ; i++) 
{ 
ImapMailboxMessage msg = mailbox.Messages[mailCount - 1]; 
msg = command.FetchBodyStructure(msg); 

msg.BodyParts[0].ContentEncoding = BodyPartEncoding.NONE; 
msg = command.FetchBodyPart(msg, msg.HTML); 

foreach (ImapMessageBodyPart a in msg.BodyParts) 
{ 
    if (a.Data != null) 
    { 
     string fileName = ""; 

     if (a.Attachment) fileName = ParseFileName(a.Disposition); 
      string mimeType = a.ContentType.MediaType.ToLower(); 

     a.ContentEncoding = BodyPartEncoding.UTF7; 
     htmlbody = a.Data; 
    } 
} 
} 

auth.Logout(); 
connection.Close(); 
+1

그리고 사람들과 세부 정보를 공유하지 마십시오. –

+0

테스트 계정입니다. 문제가 없습니다 .. –

+0

어떻게이 코드를 실행할 수 있습니까? Gmail 계정에서만 메일을 다운로드 할 수 있습니까? 다운로드 후 다운로드 한 파일을 변환 할 수 있습니까? – Nicholas

답변

1

내 선택이있다 그렇지 않은 경우 codeplex에 interimap 프로젝트가 있습니다. 첨부 파일을 완벽하게 다룹니다.

-1

짧은 기간 동안 사용하려면 chilkat IMAP API를 사용하십시오. 전체 전자 메일을 eml 파일로 저장하고 다른 사람이 실행될 수 있도록 충분한 샘플을 저장할 수 있습니다. 은 그것은 내가 EML 파일에서 첨부 파일을 읽기 위해이 사용

ImapMailboxMessage mbStructure = new ImapMailboxMessage(); 
mbStructure = command.FetchBodyStructure(a); 
for (int j = 0; j < a.BodyParts.Count; j++) 
{ 
//create dir if doesnot exist 
if (!Directory.Exists(path)) 
{ 
    DirectoryInfo di = Directory.CreateDirectory(path); 
} 
if (mbStructure.BodyParts[j].Attachment) 
{ 
    //Attachment 
    command.FetchBodyPart(mbStructure, mbStructure.BodyParts.IndexOf(mbStructure.BodyParts[j])); 
    //Write Binary File 
    FileStream fs = new FileStream(path + mbStructure.BodyParts[j].FileName, FileMode.Create); 
    fs.Write(mbStructure.BodyParts[j].DataBinary, 0, (int)mbStructure.BodyParts[j].DataBinary.Length); 
    fs.Flush(); 
    fs.Close(); 
} 
}         
0

ImapX는 최고의 라이브러리입니다. GMail과 완벽하게 작동합니다. 사용하기 쉬운 데드. 당신은 당신이 선택한 폴더에 하나 이상의 이메일이있을 때 당신은 더 나은 작품 ImapMailboxMessage msg = mailbox.Messages[i];

을 사용할 수 있습니다

ImapMailboxMessage msg = mailbox.Messages[mailCount - 1]; 

쓰기

http://hellowebapps.com/products/imapx/

0

.

[mailCount -1]은 마지막 메시지를 읽지 않습니다.

관련 문제