Mimekit can not attach file

40 Views Asked by At

I am using Mimekit email sender. I try to attach a file and all the code to do that errors out. When I add this:

var attachments = new AttachmentCollection ();
var attachment = attachments.Add(@"d:\temp\Testfile with æ ø og å.pdf");

foreach (var parameter in attachment.ContentType.Parameters)
    parameter.EncodingMethod = ParameterEncodingMethod.Rfc2047;

foreach (var parameter in attachment.ContentDisposition.Parameters)
    parameter.EncodingMethod = ParameterEncodingMethod.Rfc2047;

It fails. Below is the base code. 

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                          | SecurityProtocolType.Tls11
                                          | SecurityProtocolType.Tls12;
                var email = new MimeMessage();


                email.From.Add(new MailboxAddress("WCS", "***@aol.com"));
                email.To.Add(new MailboxAddress("Receiver Dan", "***.com"));
                email.Subject = "Testing out email sending";
                email.Body = new TextPart(MimeKit.Text.TextFormat.Plain)
                {
                    Text = "Hello all the way from the land of C#"
                };

                var smtp = new SmtpClient();
                smtp.CheckCertificateRevocation = false;
                smtp.AuthenticationMechanisms.Remove("XOAUTH2");

                smtp.Connect("smtp.aol.com", 25, false);
                // requires generate app password on AOL site
                smtp.Authenticate("[email protected]", "fcdx\r\nvkrz\r\nyyft\r\nsdgy");

                try
                {
                    smtp.Send(email);
                    smtp.Disconnect(true);
                    Console.WriteLine("Email Sent Successfully.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
                var client = new SmtpClient();

            }
0

There are 0 best solutions below