我正在尝试学习如何使用MailKit库,但我正在努力检索附件.到目前为止,我的代码将打开一个邮箱,浏览每个邮件并存储数据,如发件人,主题,正文,日期等,但我不能处理附件.
我试图在github和其他网站上使用其他人解决方案,但我仍然不明白他们在代码中做了什么,当我接近获得解决方案时,它会导致更多错误,所以我会感到压力并删除所有代码.我并不是说看起来很懒,但如果有人能解释我是如何做到的,我会很高兴.我基本上是在为Web表单应用程序构建一个邮件客户端.
下面是我的代码,所以你可以看到我相当无能:)
// Open the Inbox folder
client.Inbox.Open(FolderAccess.ReadOnly, cancel.Token);
//get the full summary information to retrieve all details
var summary = client.Inbox.Fetch(0, -1, MessageSummaryItems.Full, cancel.Token);
foreach (var msg in summary)
{
//this code originally downloaded just the text from the body
var text = msg.Body as BodyPartText;
//but I tried altering it so that it will get attachments here also
var attachments = msg.Body as BodyPartBasic;
if (text == null)
{
var multipart = msg.Body as BodyPartMultipart;
if (multipart …
Run Code Online (Sandbox Code Playgroud) 我正在使用 MailKit 从 Gmail 帐户读取邮件。效果很好。但是,我想获取消息状态,如已读、未读、重要、已加星标等。MailKit 可以吗?我似乎找不到任何关于它的信息。
这是我的代码:
var inbox = client.Inbox;
var message = inbox.GetMessage(4442);//4442 is the index of a message.
Console.WriteLine("Message Importance : {0}", message.Importance);
Console.WriteLine("Message Priority : {0}", message.Priority);
Run Code Online (Sandbox Code Playgroud)
重要性和优先级总是返回“正常”。如何找到这条消息被标记为重要或不重要?以及如何获取此消息的已读或未读状态?。
Visual Studio 2015 中的 0 代码
1 我正在使用 Mailkit 最新版本 (1.18.1.1) 从我自己的电子邮件服务器发送电子邮件。
2 电子邮件服务器具有不受信任的自签名证书。
3 我在代码中添加了以下两行,以忽略服务器证书错误:
client.ServerCertificateValidationCallback = (mysender, certificate, chain, sslPolicyErrors) => { return true; };
client.CheckCertificateRevocation = false;
Run Code Online (Sandbox Code Playgroud)
4 但我的程序仍然崩溃。
5 在电子邮件服务器日志中显示错误:
来自未知 [xxx.xxx.xxx.xxx] 的 SSL_accept 错误:连接被对等方重置
我猜这是因为服务器证书问题。因为在 Wireshark 捕获中,一旦我获得 SERVER 证书,连接就会终止。
6 我还在我的系统中安装了不受信任的电子邮件服务器证书,但问题仍然存在。
8 完整代码:
使用 (var client = new SmtpClient(new ProtocolLogger("logging.log")))
{
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (mysender, certificate, chain, sslPolicyErrors) => { return …
Run Code Online (Sandbox Code Playgroud) 我正在使用 NLog 目标来使用 smtp.google.com:587 SMTP 服务器将日志发送到一些电子邮件。当我使用标准 System.Net.Mail.SmtpClient 库时,它可以正常工作,但是在使用 MailKit 时,它会抛出错误:
发送邮件时出错。异常:>System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10061): 无法建立连接,因为目标机器主动拒绝它。>[::ffff:173.194.76.108]:587
我添加了一个带有两个控制台项目的解决方案。一种使用 MailKit 和其他 System.Net.Mail。所有设置参数看起来都一样,但它在 MailKit 中不起作用:
using System;
using System.Diagnostics;
using MailKit.Net.Smtp;
using MailKit;
using MailKit.Security;
using MimeKit;
namespace SendMailKit
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Create message");
var message = new MimeMessage();
message.From.Add(new MailboxAddress("GTS", "tfs.gettaxsolutions@gmail.com"));
message.To.Add(new MailboxAddress("Me", "info@gettaxsolutions.com"));
message.Subject = "Test mail";
message.Body = new TextPart("plain")
{
Text = @"Hello, This is the test"
};
Console.WriteLine("Create SMTP client");
using (var client = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 imap 访问 gmail 电子邮件,并且代码在 ssl 握手时失败,但没有显示任何错误。如果有人可以帮忙解决这个问题,真的很感激。我已经使用 xunit,.NET Core 2.1 构建了它。我正在使用 MailKit Nuget
public GMailHandler(string mailServer, int port, bool ssl, string login, string password)
//mailServer = imap.gmail.com
//port = 993
//ssl = true
{
if (ssl)
Client.Connect(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Authenticate(login, password);
Client.Inbox.Open(FolderAccess.ReadOnly);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用MailKit访问IMAP帐户(由jstedfast创建)
我设法下载消息(作为MimeMessage),在某些时候我需要将它"转发"到另一个帐户.
如何最好的方式来保存原始电子邮件的所有信息(地址,标题,正文内容等).
谢谢!
我有一个控制台应用程序,我安装了mailkit包用于消息传递.
我在main方法中有代码来测试mailkit smtp客户端.我有运行smtp4dev虚拟服务器,客户端代码是github中mailkit的示例代码,其中认证部分已注释,主机是localhost,端口26与smtp4dev配置匹配.
执行客户端代码时,smtp4dev stop running
和未处理的异常发生,IOException: Unable to read data from the transport connection: an existing connection was forcibly closed by the remote host.
如何配置smtp4dev以从mailkit客户端接收消息?
有没有办法只过滤带有附件的电子邮件?我正在使用这个代码
using (var client = new ImapClient())
{
client.Connect(IMAPServer, IMAPport, IMAPSSL);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(User, Password);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
//filter email with attachments only
var results = inbox.Search(SearchQuery.NotSeen.And(SearchQuery.NotDeleted));
}
Run Code Online (Sandbox Code Playgroud) 我在 Microsoft 网站上读到 SmtpClient 已过时,他们建议使用 MailKit 替换它。
我正在编写一个应用程序来使用 MailKit。
这是我到目前为止:
// *************** SEND EMAIL *******************
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
//accept all SSL certificates
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect(emailSettings.SmtpServer, emailSettings.SmtpPort, emailSettings.IsSslEnabled);
if (emailSettings.IsAuthenticationRequired)
{
// Note: only needed if the SMTP server requires authentication
client.Authenticate(emailSettings.SmtpUsername, emailSettings.SmtpPassword);
}
// timeout = 20 seconds
client.Timeout = 20000;
client.Send(message);
client.Disconnect(true);
}
Run Code Online (Sandbox Code Playgroud)
当我设置这部分时:
client.Connect(emailSettings.SmtpServer, emailSettings.SmtpPort, emailSettings.IsSslEnabled);
Run Code Online (Sandbox Code Playgroud)
最后一个参数是bool useSSL
,我将其设置为 true。我的电子邮件服务器由 Rackspace 托管,所以我知道它使用 SSL。当我将此选项设置为 true 时,它无法发送,但如果我将此选项设置为 false,则发送正常。
此行不应该捕获证书类型: …
我一直在使用 Mailkit 2.15,现在尝试升级到 v3.4.1。当我升级时,它的所有依赖项都已安装,包括 System.Runtime.CompilerServices.Unsafe v4.5.3。但是当我执行代码时,出现以下异常。
13-Oct-2022 16:33:19,303 [INFO ] Mail SendEmail - System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.Span`1..ctor(T[] array)
at MimeKit.Utils.ValueStringBuilder..ctor(Int32 initialCapacity)
at MimeKit.Utils.Rfc2047.Encode(FormatOptions options, Encoding charset, String text, Boolean phrase)
at MimeKit.Header.EncodeUnstructuredHeader(ParserOptions options, FormatOptions format, Encoding encoding, String field, String value)
at MimeKit.Header.EncodeAddressHeader(ParserOptions options, FormatOptions …
Run Code Online (Sandbox Code Playgroud)