2

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:

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. It says:

Argument 1: cannot convert from 'MimeKit.MimeMessage' to ' System.Net.Mail.MailMessage'

How can I send this email correctly?

I tried what is was told here : Can I send files via email using MailKit?

But I couldn't do it for same error

  • 1
    Are you using the SMTP Client in MimeKit or the one in System.Net.Mail? Given the error it's the latter. Why aren't you use MailKit.Net.Smtp.SmtpClient? – phuzi May 31 '22 at 15:33
  • MimeKit does not give me a SMTP Client, I don't know why, I followed same steps of this video https://www.youtube.com/watch?v=S8tLIG20qF8&t=376s (Send Email using MimeKit and MailKit Package) –  May 31 '22 at 15:36
  • 1
    Can you post your "using ... " section? – Fildor May 31 '22 at 15:37
  • Sorry, I'm not going to watch a video to figure out what you're trying to do! – phuzi May 31 '22 at 15:37
  • _"MimeKit does not give me a SMTP Client,"_ - How come? What happens when you try? – phuzi May 31 '22 at 15:41
  • Yes, It was to show where I got the information –  May 31 '22 at 15:45
  • Have you added MailKit and added `using MailKit.Net.Smtp;` to your file? – phuzi May 31 '22 at 15:47
  • Thank you everyone, I had only MimeKit but not MailKit, my bad –  May 31 '22 at 15:48

1 Answers1

3

The error indicates that you are using the wrong SMTP client!

You should be using the one that comes with MimeKit (MailKit.Net.Smtp.SmtpClient) and not the one in .NET (System.Net.Mail.SmtpClient)

Note that MimeKit and MailKit work hand-in-hand with MimeKit devoted to the parsing and handling of messages. Where as MailKit concerns the network aspects of sending and receiving messages.

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • The type or namespace name 'MailKit' could not be found (are you missing a using directive or an assembly reference?) I got this –  May 31 '22 at 15:43
  • Have you got the appropriate using? `using MailKit.Net.Smtp;` notice that this is MailKit (which is part of MimeKit) – phuzi May 31 '22 at 15:46
  • It was not downloaded, I only had MimeKit but not MailKit... –  May 31 '22 at 15:46