I am trying to send an email with MimeKit in the following way:
private bool EnviarMail(string file, string from, string to, string subject, string contenido, string nombre)
{
bool estado = false;
try
{
var mensaje = new MimeMessage();
mensaje.From.Add(new MailboxAddress(nombre, from));
mensaje.To.Add(new MailboxAddress("", to));
mensaje.Subject = subject;
//var bodyBuilder = new BodyBuilder();
//bodyBuilder.HtmlBody = contenido;
//bodyBuilder.Attachments.Add(file);
//mensaje.Body = bodyBuilder.ToMessageBody();
var body = new TextPart()
{
Text = contenido
};
using (var client = new SmtpClient("Host", puerto))
{
client.Send(mensaje);
}
estado = true;
return estado;
}
catch (Exception ex)
{
return estado;
}
}
But I have the following error inclient.Send(mensaje);
Argument 1: cannot convert from 'MimeKit.MimeMessage' to ' System.Net.Mail.MailMessage'
I understand what the error is telling me but I don't know how to change the code to make it work correctly.
There are two problems here
TOPIC 1: Attach a PDF file in MimeMessage
In order to build an email with [MimeMessage] ( http://www.mimekit.net/docs/html/T_MimeKit_MimeMessage.htm ) you have to help yourself with the Add method of the BodyBuilder.Attachments collection of attachments , which has 7 overloads ( you can pass byte[], stream, etc)
And at the end you should use the BodyBuilder.ToMessageBody() method to build the body
that what it does textual help
Example in your code, getting "physically" the PDF file (but it can come from the DB, API, storage, etc)
TOPIC 2: Error in Send of SmtpClient
Regarding sending with SmtpClient that you get the error, it is because you have to use MailKit.Net.Smtp.SmtpClient which is what Microsoft recommends to use instead of System.Net.Mail.SmtpClient
Links that can help you
I hope it helps or guides you.
Why don't you try it with MailMessage. Here is some code that should work.