如何通过imap获取gmail的附件。 [英] How to get attachments of gmail through imap.
本文介绍了如何通过imap获取gmail的附件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用AE.NET通过IMAP从Gmail获取邮件。我能够获取消息,但是当我尝试迭代消息附件时,没有消息。返回message.Value.Attachments.Count()给我0.请看下面的代码:
using (ImapClient ic = new ImapClient( imap.gmail.com, test ***** @ gmail。 com, ******,ImapClient.AuthMethods.Login, 993 , true ))
{
// 打开邮箱,不区分大小写
ic.SelectMailbox( INBOX);
// 根据标记Undeleted()获取消息。
Lazy< MailMessage> [] messages = ic.SearchMessages(SearchCondition.Undeleted(), false );
int count = messages.Count();
Response.Write(count.ToString());
// 处理每条消息
foreach (Lazy< MailMessage> message in messages)
{
MailMessage m = message.Value;
Response.Write( < br />);
Response.Write(m.From.Address);
Response.Write( ---);
Response.Write(m.Subject);
Response.Write( ***);
Response.Write(m.MessageID);
Response.Write( %%%);
Response.Write(m.From.DisplayName);
if (m.Attachments.Count > 0 )
{
// ExtractString(m。主题,(,));
foreach (附件附件 in m.Attachments)
{
string fileName = m.Subject + _ + attachment.Filename;
attachment.Save( @ D:\ DemoMail \ + fileName + Path .GetExtension(attachment.Filename));
}
}
}
}
解决方案
这段代码将是我已经开发和测试的完整版本的vb版本,只是将此代码转换为c#,所以我将发布vb代码以供参考。它包括用于保存所选文件夹附件的片段,以及将CSV格式的cc和bcc保存为字符串变量:
VB代码 < br $> b $ b
公共 Sub fetchMails()
Dim imapClient As 新 Imap4Client()
' 登录邮件
Dim mailNumber 作为 整数 = 10 阅读第10封邮件
Dim notYetProcessedBox As Mailbox = imapClient.SelectMailbox(收件箱)
Dim msg As Message = notYetProcessedBox.Fetch.MessageObject(mailNumber)
' 阅读cc和bcc
Dim mailCC 作为 字符串 =
Dim mailBcc 作为 字符串 =
Dim cc() As ActiveUp。 Net.Mail.Address = msg.Cc.ToArray()
Dim bcc() As ActiveUp.Net.Mail.Address = msg.Bcc.ToArray()
对于 每个地址作为 ActiveUp .Net.Mail.Address 在 cc
mailCC& = address.ToString
Next
对于 每个地址 As ActiveUp.Net.Mail.Address 在 bcc
mailBcc& = address.ToString
下一步
' 用于保存附件
If msg.Attachments.Count<> 0 然后
如果系统.IO.Directory.Exists( ../ email_attachment /)= False 然后
System.IO.Directory.CreateDirectory( ../ email_attachment /)
msg.Attachments.StoreToFolder( ../ email_attachment /)
结束 如果
结束 如果
结束 Sub
C#代码
public void fetchMails()
{
Imap4Client imapClient = new Imap4Client();
// 登录邮件
int mailNumber = 10 ;
// 阅读第10封邮件
邮箱notYetProcessedBox = imapClient.SelectMailbox( 收件箱);
消息msg = notYetProcessedBox.Fetch.MessageObject(mailNumber);
// 阅读cc和bcc
string mailCC = ;
string mailBcc = ;
ActiveUp.Net.Mail.Address [] cc = msg.Cc.ToArray();
ActiveUp.Net.Mail.Address [] bcc = msg.Bcc.ToArray();
foreach (ActiveUp.Net.Mail.Address地址 in cc){
mailCC + = address.ToString;
}
foreach (ActiveUp.Net.Mail.Address地址 in bcc) {
mailBcc + = address.ToString;
}
// 用于保存附件
if (msg.Attachments.Count!= 0 ){
if (System.IO.Directory.Exists( ../ email_attachment /) == false ){
System.IO.Directory.CreateDirectory( ../ email_attachment /跨度>);
msg.Attachments.StoreToFolder( ../ email_attachment /);
}
}
}
这篇文章应该让你去
使用C#的IMAP客户端库 [ ^ ]
I am using AE.NET to get mail from Gmail using IMAP. I am able to get the messages, however when I try iterate over the message attachments there are none. Returning message.Value.Attachments.Count() gives me 0. Please see my code below:
using (ImapClient ic = new ImapClient("imap.gmail.com", "test*****@gmail.com", "******", ImapClient.AuthMethods.Login, 993, true))
{
// Open a mailbox, case-insensitive
ic.SelectMailbox("INBOX");
// Get messages based on the flag "Undeleted()".
Lazy<MailMessage>[] messages = ic.SearchMessages(SearchCondition.Undeleted(), false);
int count = messages.Count();
Response.Write(count.ToString());
// Process each message
foreach (Lazy<MailMessage> message in messages)
{
MailMessage m = message.Value;
Response.Write("<br/>");
Response.Write(m.From.Address);
Response.Write("---");
Response.Write(m.Subject);
Response.Write("***");
Response.Write(m.MessageID);
Response.Write("%%%");
Response.Write(m.From.DisplayName);
if (m.Attachments.Count > 0)
{
// ExtractString(m.Subject, "(", ")");
foreach (Attachment attachment in m.Attachments)
{
string fileName = m.Subject + "_" + attachment.Filename;
attachment.Save(@"D:\DemoMail\" + fileName + Path.GetExtension(attachment.Filename));
}
}
}
}
解决方案
This code will be help full i had developed and tested vb version only just converted this code to c#, so i will post the vb code for reference. it includes snippets for saving attachments to selected folder and also for save cc and bcc in CSV format to a string variable:
VB Code
Public Sub fetchMails() Dim imapClient As New Imap4Client() ' login to the mail Dim mailNumber As Integer = 10 'reading 10th mail Dim notYetProcessedBox As Mailbox = imapClient.SelectMailbox("Inbox") Dim msg As Message = notYetProcessedBox.Fetch.MessageObject(mailNumber) ' To read cc and bcc Dim mailCC As String = "" Dim mailBcc As String = "" Dim cc() As ActiveUp.Net.Mail.Address = msg.Cc.ToArray() Dim bcc() As ActiveUp.Net.Mail.Address = msg.Bcc.ToArray() For Each address As ActiveUp.Net.Mail.Address In cc mailCC &= address.ToString Next For Each address As ActiveUp.Net.Mail.Address In bcc mailBcc &= address.ToString Next ' For saving attachment If msg.Attachments.Count <> 0 Then If System.IO.Directory.Exists("../email_attachment/") = False Then System.IO.Directory.CreateDirectory("../email_attachment/") msg.Attachments.StoreToFolder("../email_attachment/") End If End If End Sub
C# Code
public void fetchMails() { Imap4Client imapClient = new Imap4Client(); // login to the mail int mailNumber = 10; //reading 10th mail Mailbox notYetProcessedBox = imapClient.SelectMailbox("Inbox"); Message msg = notYetProcessedBox.Fetch.MessageObject(mailNumber); // To read cc and bcc string mailCC = ""; string mailBcc = ""; ActiveUp.Net.Mail.Address[] cc = msg.Cc.ToArray(); ActiveUp.Net.Mail.Address[] bcc = msg.Bcc.ToArray(); foreach (ActiveUp.Net.Mail.Address address in cc) { mailCC += address.ToString; } foreach (ActiveUp.Net.Mail.Address address in bcc) { mailBcc += address.ToString; } // For saving attachment if (msg.Attachments.Count != 0) { if (System.IO.Directory.Exists("../email_attachment/") == false) { System.IO.Directory.CreateDirectory("../email_attachment/"); msg.Attachments.StoreToFolder("../email_attachment/"); } } }
This article should get you going
IMAP Client library using C#[^]
这篇关于如何通过imap获取gmail的附件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文