1

We are using MailKit/MimeKit to relay emails. when try to send mail throw error like 'Unexpected token at offset 37' Please help to resolve this.

Trace message as below

at MimeKit.MailboxAddress.set_Address(String value)
at MimeKit.MailboxAddress..ctor(Encoding encoding, String name, String address)
using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
{
    smtpClient.Connect(mailServer, Port, MailKit.Security.SecureSocketOptions.None);
    smtpClient.Send(mailMessage);
    smtpClient.Disconnect(true);
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
Ezhumalai
  • 163
  • 1
  • 2
  • 10
  • 2
    Looks like the email address has invalid content. It must be a pure address: user@domain – Peter B Mar 26 '21 at 11:26
  • What port number are you using? Does email have to be secure on your email server? The unexpected token can occur if the server cannot decrypt the email. So it sound like you may be using the wrong port number. Also if you are inside a corporate network you cannot send email through the firewall. Try application at home without a firewall. – jdweng Mar 26 '21 at 12:31
  • I just hit this problem. I send email in body from browser and for some browsers the server throws this error. No idea what is going on – Александър К. May 06 '22 at 22:36

1 Answers1

5

Like Peter B said, the issue is that the email address that your program is passing to the MailboxAddress constructor is malformed in some way. Make sure that the address parameter is of the form user@example.com.

If the string is something more like User Name <user@example.com>, then I would recommend using MailboxAddress.Parse (emailAddr) instead.

jstedfast
  • 35,744
  • 5
  • 97
  • 110