Home > OS >  Cannot sent mail in Mimekit
Cannot sent mail in Mimekit

Time:10-20

While sending the mail from c# using the below code of MimeKit

var message = new MimeMessage();
message.From.Add(new MailboxAddress(FromAddress, "Notification"));
            

foreach (var address in Toaddress.Split(','))
{
  message.To.Add(new MailboxAddress(address.Trim(), ""));
}
message.Subject = Subject;

message.Body = new TextPart("plain") { Text = "Test Message" };

using (var client = new SmtpClient())
{
   client.Connect(EmailHostName, Portnumber, SecureSocketOptions.StartTls);
   client.Authenticate(UserName, Password);

   client.Send(message);
   client.Disconnect(true);
}

I get the below exception

MimeKit.ParseException: Invalid addr-spec token at offset 0
   at MimeKit.InternetAddress.TryParseAddrspec(Byte[] text, Int32& index, Int32 endIndex, Byte[] sentinels, Boolean throwOnError, String& addrspec, Int32& at)
   at MimeKit.MailboxAddress.set_Address(String value)
   at MimeKit.MailboxAddress..ctor(Encoding encoding, String name, String address)
   at MimeKit.MailboxAddress..ctor(String name, String address)

I had tried different solutions from Internet but neither worked, Anyone please help to solve the issue

Tried Solutions:

Unable to parse tnef part from MimeMessage

https://www.csharpcodi.com/csharp-examples/MimeKit.InternetAddress.TryParseLocalPart(byte[], ref int, int, bool, out string)/ (No use)

https://www.nopcommerce.com/en/boards/topic/90019/email-error-invalid-addr-spec-token-at-offset-0-v43 (No Use)

Edit: The issue is because of message.From.Add(new MailboxAddress(FromAddress, "Notification")); it is in wrong order so changed to message.From.Add(new MailboxAddress("Notification",FromAddress));to over come the error

CodePudding user response:

I think you just have your MailboxAddress ctor arguments in wrong order.

https://github.com/jstedfast/MimeKit/blob/master/MimeKit/MailboxAddress.cs#L163

public MailboxAddress (string name, string address) { ... }
  • Related