简体   繁体   English

如何在 C# 中使用 MimeKit 发送 email

[英]How can I send an email with MimeKit in C#

Hi everyone I'm trying to send an email using the MimeKit but I'm trying to attach a.pdf file I have some problems:大家好我正在尝试使用 MimeKit 发送一个 email 但我正在尝试附加一个 .pdf 文件我有一些问题:

This is how I'm trying to send it:这就是我尝试发送它的方式:

 private bool EnviarMail(string file, string from, string to, string subject, string content, string name)
    {
        bool estado = false;

        try
        {
            var mensaje = new MimeMessage();
            mensaje.From.Add(new MailboxAddress(name, from));
            mensaje.To.Add(new MailboxAddress("", to));
            mensaje.Subject = subject;

            var bodyBuilder = new BodyBuilder();
            bodyBuilder.HtmlBody = content;
            bodyBuilder.Attachments.Add(file);
            mensaje.Body = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient("myHost", myPort))
            {
                client.Send(mensaje);    
            }

            estado = true;
            return estado;
        }
        catch (Exception ex)
        {
            return estado;
        }
    }

But I have this error on client.send(mensaje) line.但是我在client.send(mensaje)行有这个错误。 It says:它说:

Argument 1: cannot convert from 'MimeKit.MimeMessage' to ' System.Net.Mail.MailMessage'参数 1:无法从“MimeKit.MimeMessage”转换为“System.Net.Mail.MailMessage”

How can I send this email correctly?我怎样才能正确发送这个 email?

I tried what is was told here: Can I send files via email using MailKit?我尝试了这里所说的: Can I send files via email using MailKit?

But I couldn't do it for same error但是我不能因为同样的错误而做

The error indicates that you are using the wrong SMTP client!报错说明你用错了SMTP客户端!

You should be using the one that comes with MimeKit ( MailKit.Net.Smtp.SmtpClient ) and not the one in .NET ( System.Net.Mail.SmtpClient )您应该使用 MimeKit 附带的那个 ( MailKit.Net.Smtp.SmtpClient ) 而不是 .NET ( System.Net.Mail.SmtpClient ) 中的那个

Note that MimeKit and MailKit work hand-in-hand with MimeKit devoted to the parsing and handling of messages.请注意,MimeKit 和 MailKit 与 MimeKit 携手合作,致力于消息的解析和处理。 Where as MailKit concerns the.network aspects of sending and receiving messages. MailKit 关注发送和接收消息的网络方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中发送包含带参数链接的电子邮件? - How can I send an email containing a link with parameter in C#? 如何在 Unity 中使用 C# 脚本发送电子邮件? - How can I send an email with a C# script in Unity? 如何在C#上通过电子邮件发送Excel文件? - How can I send an Excel file by email on C#? 我如何发送“ <asp:datalist> ”将c#的内容发送到电子邮件? - How can I send “<asp:datalist>” content with c# to an email? 如何使用C#在电子邮件应用程序(mailkit和mimekit)中存储和读取数据库中的数字证书? - How to store and read Digital Certificates in database using C# for email application (mailkit and mimekit)? 如何使用 cPanel email 帐户在 C# 中发送 email - How can I send an email in C# using cPanel email account 我可以向C#中的多个收件人发送多个电子邮件地址吗? - can i send multiple email addresses to multiple recipient in C# C#如何发送邮件? - C# how to send email? 如何像 dygraph js 那样从包含图形的 c# 发送电子邮件? - How can i send email from c# that contain graphics as dygraph js does? 我如何在ex.html c#中使用extern html文件作为附件并将其发送到电子邮件地址? - How I can use a extern html file for a attachment in asp.net c# and send it to a email address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM