我正在使用 MailKit .net 库从 gmail 获取电子邮件。
根据从 IMAP 服务器和IMAP获取新邮件:搜索 UID 大于 X 的邮件(或通常在我上次搜索之后)
我想搜索上次记住 UID 之后的所有电子邮件。假设获取的最后一条消息的唯一 ID 为“123456”。我要搜索
123456:*
如何在 MailKit 中实现此搜索?
我想根据多个条件(如 NotSeen 和 NotDeleted)使用 mailkit 搜索 imap 收件箱。我知道查询是容易接受的,如果我们进行单独的搜索查询。
var uids = client.Inbox.Search(SearchQuery.NotSeen);
var uids = client.Inbox.Search(SearchQuery.NotDeleted );
Run Code Online (Sandbox Code Playgroud)
但我需要将这两个查询放在一起并根据条件获取所有 Uid。任何帮助,将不胜感激。
public byte[] Attachment { get; set; }
是我想添加到草稿保存中的附件。有人知道如何保存byte[]
草稿附件吗?
public void DraftMessage(string strto, string strcc, string strBcc,
string strSubject, string strBody, List<UserAttachment> listAttachments)
{
try
{
MimeMessage email = new MimeMessage();
email.MessageId = MimeUtils.GenerateMessageId();
var list = new InternetAddressList();
if (strto != "")
{
string[] strArrayto = strto.Split(';');
if (strArrayto != null)
{
list = new InternetAddressList();
foreach (string _strTo in strArrayto)
list.Add(new MailboxAddress(_strTo));
email.To.AddRange(list);
}
}
if (strcc != "")
{
string[] strArraycc = strcc.Split(';');
if (strArraycc != …
Run Code Online (Sandbox Code Playgroud) 我看到MimeKit.MailboxAddress
有一种TryParse
方法,如果它被覆盖,可以使用它来验证某些字符串输入是电子邮件地址吗?这些文档非常混乱,TryParse
单独解析!realEmail!com"
就可以了。
需要明确的是,我不需要验证电子邮件是否确实存在,只需验证其是否有效即可。我还想避免使用正则表达式,只是确保电子邮件在 MimeKit 看来是有效的...我是否必须创建一条假消息并尝试添加地址才能做到这一点?似乎太过分了。System.Net.Mail 只会通过无效电子邮件的异常,这很好,因为我可以只使用 try/catch。
我有一个控制台应用程序,它所做的就是循环所有客户并向特定客户发送电子邮件,然后关闭。我注意到 MailKit\xe2\x80\x99s 中的一些函数提供异步功能,因此我尝试使用它们而不是非 aysnc,当我这样做时,它执行了第一个语句 (emailClient.ConnectAsync),然后我注意到我的控制台应用程序正在关闭。它没有\xe2\x80\x99t崩溃。在调用 SendReports() 函数后,执行返回到 Main() 并继续执行。
\n\nprivate static void Main(string[] args)\n{\n ...\n var reportServices = new ReportsServices();\n\n reportServices.SendReportsToCustomers();\n\n Log.CloseAndFlush(); // It executes the first await call then returns here.\n}\n\ninternal class ReportsServices\n{\n public async void SendReportsToCustomers()\n {\n try\n {\n foreach (var customer in _dbContext.Customer)\n {\n ...\n await SendReport(customer.Id, repTypeId, freqName);\n }\n }\n catch (Exception e)\n {\n Console.WriteLine(e);\n throw;\n }\n }\n\n private async Task SendReport(int customerId, int repTypeId, string freqName)\n {\n try\n {\n ...\n var es = new …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 MailKit 发送带有嵌入图像的电子邮件。它在 MS Outlook 上运行良好。但是,图像不会在 Gmail 中显示为嵌入图像。我尝试附加“data:image/png;base64”格式,它也适用于 Outlook,但不适用于 Gmail。
任何帮助将非常感激!
public string SendEmail (MyModel myModel)
{
var message = new MimeMessage();
var bodyBuilder = new BodyBuilder
{
HtmlBody = myModel.Body
};
GetAllImgTags(bodyBuilder);
message.Body = bodyBuilder.ToMessageBody();
// Send Email Here...
return "OK"
}
public string GetAllImgTags(BodyBuilder bodyBuilder)
{
HtmlDocument document = new HtmlDocument();
document.LoadHtml(bodyBuilder.HtmlBody);
var imgList = document.DocumentNode.Descendants("img").Where(x =>
{
string src = x.GetAttributeValue("src", null) ?? "";
return !string.IsNullOrEmpty(src);
}).ToList();
foreach(var item in imgList)
{
string currentSrcValue = item.GetAttributeValue("src", null); …
Run Code Online (Sandbox Code Playgroud) 最近,我在我们的一个旧应用程序中遇到了问题。突然,应用程序停止向其用户发送注册/重置密码电子邮件。经过一些调试,我发现原因是 Mailkit SmtpClient 类的奇怪行为。问题出现在以下代码中:
public async Task Execute(EmailMessage email)
{
var mimeMessage = CreateMimeMessageFromEmailMessage(email);
using var smtpClient = new SmtpClient();
await smtpClient.ConnectAsync(Options.SmtpServer, Options.Port, MailKit.Security.SecureSocketOptions.None);
await smtpClient.AuthenticateAsync(Options.Username, Options.Password);
await smtpClient.SendAsync(mimeMessage);
await smtpClient.DisconnectAsync(true);
}
Run Code Online (Sandbox Code Playgroud)
每次在 ConnectAsync() 方法中都会发生错误,但是,它不会抛出任何异常,只是在该方法中冻结一段时间(大约一分钟),然后它只是抛出任务已取消,没有任何 mailkit 特定的信息。
起初,我认为服务器地址、端口或安全选项可能是错误的,但我们在其他一些应用程序中使用相同的设置(和相同的代码!),并且它们似乎工作正常。它也不应该是由我的电脑/网络配置中的某些内容引起的,因为其他开发人员在处理此程序时也会遇到相同的错误。我还尝试对不同的电子邮件使用选项,但错误仍然存在。
知道如何找到此错误的原因,如何在没有抛出异常时调试问题等吗?
我能够使用 MailKit 和 MimeKit 发送 SMTP 电子邮件,而 Outlook 是接收这些邮件的客户端工具。下面的代码已被使用,我的收件箱已收到电子邮件。
var email = new MimeMessage
{
Sender = MailboxAddress.Parse("<<from>>")
};
email.To.Add(MailboxAddress.Parse("<<to>>"));
email.Subject = "Test mail from Jaish";
var builder = new BodyBuilder();
builder.TextBody = "This is a test mail from Jaish Mathews";
email.Body = builder.ToMessageBody();
using var smtp = new SmtpClient();
smtp.LocalDomain = "<<domain>>";
smtp.Timeout = 10000;
smtp.Connect("<<host>>", 25, SecureSocketOptions.None);
var mailboxes = email.To.Mailboxes;
//Sending email
await smtp.SendAsync(email);
//Disconnecting from smtp
smtp.Disconnect(true);
Run Code Online (Sandbox Code Playgroud)
问题是我的“已发送”文件夹没有记录这些已发送的电子邮件。如何手动复制到我的“已发送”文件夹”
我正在尝试使用 MailKit 库从我的邮箱中读取电子邮件。不幸的是,程序抛出了这个 MailKit.Security.AuthenticationException: 'LOGIN failed.'。凭据匹配(我尝试通过浏览器登录)。我正在尝试使用 MailKit 库从我的邮箱中读取电子邮件。不幸的是该程序抛出了这个
MailKit.Security.AuthenticationException:“登录失败。”
我将不胜感激任何帮助,我不知道该怎么办。我用谷歌搜索,尝试了这个但是当我更改为client.Connect("imap.outlook.com", 993, SecureSocketOptions.None);
时没有出现任何结果,抛出了相同的异常SecureSocketOptions
Auto
using (var client = new ImapClient())
{
using (var cancel = new CancellationTokenSource())
{
client.Connect("imap.outlook.com", 993, true);
//client.AuthenticationMechanisms.Remove("XOAUTH");
client.Authenticate("mail@test.cz", "password", cancel.Token);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Recent);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
Console.WriteLine("Subject: {0}", message.Subject);
}
var query = SearchQuery.DeliveredAfter(DateTime.Parse("2013-01-12"))
.And(SearchQuery.SubjectContains("MailKit"))
.And(SearchQuery.Seen);
foreach (var uid in …
Run Code Online (Sandbox Code Playgroud) 我在使用多线程使用 MailKit 的 GetFolder 函数时遇到问题。情况是他们我正在运行一个线程来移动我的电子邮件(其中一个函数确实使用 GetFolder 来查找我的源文件夹和目标文件夹以移动电子邮件),另一个线程将搜索电子邮件第一个正在运行(第二个线程也执行 GetFolder 函数)。
大多数情况下,我都会收到 InvalidOperationException 异常,告诉我“ImapClient 当前正忙于处理命令”。
我怎样才能确保这不会发生?我尝试将所有 GetFolder 和其他 MailKit 操作更改为异步,并等待任务完成,但没有任何运气或多或少同时执行此操作。
我为两个线程使用相同的 ImapClient。
我有此代码已成功发送电子邮件-除了前一天没有发送电子邮件。因此,我想检查SMTP响应,但不确定该怎么做。
现在是我的代码:
using (var client = new SmtpClient())
{
client.LocalDomain = "xxxxxxxxxxxx";
await client.ConnectAsync("xxxxxxxxxxxx", xx, SecureSocketOptions.StartTls).ConfigureAwait(false);
await client.AuthenticateAsync( new System.Net.NetworkCredential("xxxxxxxxxxxx", "xxxxxxxxxxxx")).ConfigureAwait(false);
await client.SendAsync(emailMessage).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)
因此,我在这里阅读了onMessageSent,或者可以使用MessageSent函数来查看是否有响应-我真的很想看一个示例代码,但是如何在代码中使用这些函数来确定消息是否为真的收到了吗?
我确实具有包含作为的异步发送功能的函数public void
,并且警告抑制消除了有关未等待呼叫的VisualStudio投诉。
public void SendEmail(string HtmlEmailContents, string SubjectLine, string CustomFromAddress = null, string CustomEmailRecipients = null)
{
string To = getRecipientString(mainRecipients);
string Cc = getRecipientString(ccRecipients);
string Bcc = getRecipientString(bccRecipients);
if(CustomEmailRecipients != null && CustomEmailRecipients != "")
{
To = CustomEmailRecipients;
Cc = "";
Bcc = "";
}
string finalizedFromAddress; …
Run Code Online (Sandbox Code Playgroud) 当 MailKit SmtpClient.Send() 无法发送到无效电子邮件时,我想获取并处理异常。但它不会抛出任何异常。
我尝试故意发送到无效的电子邮件,看看是否可以捕获任何异常。MailKit SmtpClient 在发送失败时是否不会抛出任何异常?如果您想知道为什么我需要这个,是因为我想向客户端应用程序返回一条错误消息,指出某些电子邮件无效。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using MyProject.Backend.Models;
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;
namespace MyProject.Backend.Helpers
{
public class SMTPHelper
{
SMTPSettings smtpSettings;
public SMTPHelper(SMTPSettings smtpSettings)
{
this.smtpSettings = smtpSettings;
}
public List<string> Send(List<PreparedEmailModel> preparedEmails)
{
using (var emailClient = new SmtpClient())
{
var failedEmails = new List<string>();
//The last parameter here is to use SSL (Which you should!)
emailClient.Connect(smtpSettings.Host, 587);
//Remove any OAuth functionality as we won't be …
Run Code Online (Sandbox Code Playgroud)