我在线查看了其他示例,但我无法弄清楚如何从MimeMessage对象下载和存储所有附件。 我确实调查了WriteTo(),但我无法让它工作。 还想知道附件是否会根据原始文件名保存,并在电子邮件中输入。 以下是我到目前为止的情况:
using (var client = new ImapClient())
{
client.Connect(Constant.GoogleImapHost, Constant.ImapPort, SecureSocketOptions.SslOnConnect);
client.AuthenticationMechanisms.Remove(Constant.GoogleOAuth);
client.Authenticate(Constant.GoogleUserName, Constant.GenericPassword);
if (client.IsConnected == true)
{
FolderAccess inboxAccess = client.Inbox.Open(FolderAccess.ReadWrite);
IMailFolder inboxFolder = client.GetFolder(Constant.InboxFolder);
IList<UniqueId> uids = client.Inbox.Search(SearchQuery.All);
if (inboxFolder != null & inboxFolder.Unread > 0)
{
foreach (UniqueId msgId in uids)
{
MimeMessage message = inboxFolder.GetMessage(msgId);
foreach (MimeEntity attachment in message.Attachments)
{
//need to save all the attachments locally
}
}
}
}
}
答案 0 :(得分:6)
这些都在&#34;如何保存附件中的FAQ中进行了解释?&#34;部分。
以下是您在问题中发布的代码的固定版本:
// ==UserScript==
// @name j,m
// @namespace hjgfn
// @description g
// @include https://www.youtube.com/watch?v=eLuBPKSnsxc
// @version 1
// run-at document-idle
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js
// ==/UserScript==
$(function(){
$(".style-scope ytd-compact-video-renderer:contains('Empfohlenes Video')").hide();
});
一些注意事项:
using (var client = new ImapClient ()) {
client.Connect (Constant.GoogleImapHost, Constant.ImapPort, SecureSocketOptions.SslOnConnect);
client.AuthenticationMechanisms.Remove (Constant.GoogleOAuth);
client.Authenticate (Constant.GoogleUserName, Constant.GenericPassword);
client.Inbox.Open (FolderAccess.ReadWrite);
IList<UniqueId> uids = client.Inbox.Search (SearchQuery.All);
foreach (UniqueId uid in uids) {
MimeMessage message = client.Inbox.GetMessage (uid);
foreach (MimeEntity attachment in message.Attachments) {
var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
using (var stream = File.Create (fileName)) {
if (attachment is MessagePart) {
var rfc822 = (MessagePart) attachment;
rfc822.Message.WriteTo (stream);
} else {
var part = (MimePart) attachment;
part.Content.DecodeTo (stream);
}
}
}
}
}
是否client.IsConnected
。如果它没有连接,它会在Authenticate()
方法中抛出异常。如果它没有成功,它会在Connect()
方法中引发异常。如果你刚刚调用了IsConnected
2行,则无需检查Connect()
状态。inboxFolder.Unread
?如果您只想下载未读邮件,请将搜索更改为SearchQuery.NotSeen
,这样只会为您提供尚未阅读的邮件UID。IMailFolder inboxFolder = client.GetFolder(Constant.InboxFolder);
逻辑因为您不需要它。如果您要使用client.Inbox
进行搜索,请不要使用其他文件夹对象迭代结果。