2

I have purchased an authenticated SMTP package with my hosting company.

I have tested the account settings via Outlook 2010 successfully.

Outlook settings:

Email: me@domain.com Outgoing SMTP: smtp.hostingcompany.net

Logon Info:

  • user: mydomain.com_account
  • pass: password

More Settings > Outgoing Server:

  • My Outgoing Server (SMTP) Requires authentication
  • User: mydomain
  • Pass: password

I have tried all possible combinations when specifying the NetworkCredentials and I keep getting an SMTP Exception: "Faulure Sending Email" InnerException: Invalid length for a Base-64 char array.

public static void SendEmail(string To, string Subject, string Body)
    {
        try
        {
            using (SmtpClient mySmtpClient = new SmtpClient(GlobalSettings.EmailHost))
            {
                mySmtpClient.UseDefaultCredentials = false;
                //mySmtpClient.EnableSsl = false;
                mySmtpClient.Credentials = new NetworkCredential(mydomain.com_account, GlobalSettings.EmailPassword);

                MailAddress from = new MailAddress("email@mydomain");
                MailAddress to = new MailAddress(To);

                using (
                    MailMessage myMail = new MailMessage(from, to)
                    {
                        Subject = Subject,
                        SubjectEncoding = Encoding.UTF8,
                        Body = Body,
                        BodyEncoding = Encoding.UTF8,
                        IsBodyHtml = true
                    })
                {
                    mySmtpClient.Send(myMail);
                }
            }
        }
        catch (SmtpException ex)
        {
            Log.WriteLog(string.Format("[Send Mail Exception] --> SMTPException has occurred: {0}", ex.Message), LogLevel.Error);
        }
        catch (Exception ex)
        {
            Log.WriteLog(string.Format("[Send Mail Exception] --> Exception has occurred: {0}", ex.Message), LogLevel.Error);
        }
    }

I have tried three possible username login parameters for the NetworkCredential().

Thanks in advance.

EDIT: NTLM Hack

I added the following lines to my code:

FieldInfo transport = _client.GetType().GetField("transport",
    BindingFlags.NonPublic | BindingFlags.Instance);

FieldInfo authModules = transport.GetValue(_client).GetType()
    .GetField("authenticationModules",
        BindingFlags.NonPublic | BindingFlags.Instance);

Array modulesArray = authModules.GetValue(transport.GetValue(_client)) as Array;
modulesArray.SetValue(modulesArray.GetValue(2), 0);
modulesArray.SetValue(modulesArray.GetValue(2), 1);
modulesArray.SetValue(modulesArray.GetValue(2), 3);

I am now getting a new message:

Mailbox unavailable. The server response was: relay not permitted
Seany84
  • 5,284
  • 5
  • 40
  • 63
  • Can you please post the stack trace of your exception? There are a couple of places where this could pop up. A stack trace would help narrow this down. – Ragesh Nov 04 '11 at 14:15
  • @Ragesh - Just added it there. Do I need to encode the body property of the MailMessage object as I am passing in HTML? – Seany84 Nov 04 '11 at 15:03
  • I'm pretty sure you don't have to encode the body manually. I'm using HTML bodies, too, and it encodes it automatically. – Ragesh Nov 04 '11 at 18:39
  • 1
    Relay not permitted usually happens if your server doesn't like your From or To addresses (it thinks you're a spammer). Or, it could be your user account isn't being authenticated properly. In cases like this, I find that using Wireshark to take a peek at the network traffic is the quickest way to a solution. Send a mail with Outlook, then your code and identify what's different between your SMTP requests and what Outlook is doing. – Ragesh Nov 04 '11 at 18:57

2 Answers2

1

Re your second problem:

Mailbox unavailable. The server response was: relay not permitted

This happens because the server now has no acceptable method of authenticating you, and is unwilling to risk getting a reputation as an "open relay".

This is probably fixable. The problem is that the hack you're using is too heavy handed. It is overwriting every authentication module except "Digest", when all we need to do is overwrite NTLM.

Try this cut-down version:

FieldInfo transport = _client.GetType().GetField("transport",
BindingFlags.NonPublic | BindingFlags.Instance);

FieldInfo authModules = transport.GetValue(_client).GetType()
    .GetField("authenticationModules",
        BindingFlags.NonPublic | BindingFlags.Instance);

Array modulesArray = authModules.GetValue(transport.GetValue(_client)) as Array;
modulesArray.SetValue(modulesArray.GetValue(2), 1);
smh
  • 1,135
  • 1
  • 11
  • 15
  • This is really good to know. I will look at implementing your cut-down version in a future release cycle. – Seany84 Jul 27 '12 at 09:56
1

According to the following article: "Hacking" System.Net.Mail.SmtpClient, the exception could be thrown when the SmtpClient chooses to authenticate using NTLM authentication.

The article provides a fix, although as the article name suggests it is a hack.

However in the comments a poster also notes that an incorrect password can cause the same exception.

jdavies
  • 12,364
  • 3
  • 30
  • 33