12
votes

Okay, so I have this program which in essence acts as an email client for a company, it constructs the email for them and sends it out.

I've done everything on it, but when going to send the email, they get a Mailbox Unavailable. Accessed Denied - Invalid HELO Name

Interestingly though, I use the same username for the network credentials and the from part.

EDIT: Update code to what I'm now using... Now getting Failure sending mail error.

Here is my MailConst.cs class:

public class MailConst
{

    /*
     */

    public static string Username = "username";
    public static string Password = "password";
    public const string SmtpServer = "smtp.domain.co.uk";

    public static string From = Username + "@domain.co.uk";

}

and here is the use of these variables in my main class:

    public static void SendMail(string recipient, string subject, string body, string[] attachments)
    {


        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential basicCredential = new NetworkCredential(MailConst.Username, MailConst.Password, MailConst.SmtpServer);
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress(MailConst.From);

        // setup up the host, increase the timeout to 5 minutes
        smtpClient.Host = MailConst.SmtpServer;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;
        smtpClient.Timeout = (60 * 5 * 1000);

        message.From = fromAddress;
        message.Subject = subject + " - " + DateTime.Now.Date.ToString().Split(' ')[0];
        message.IsBodyHtml = true;
        message.Body = body.Replace("\r\n", "<br>");
        message.To.Add(recipient);

        if (attachments != null)
        {
            foreach (string attachment in attachments)
            {
                message.Attachments.Add(new Attachment(attachment));
            }
        }

        smtpClient.Send(message);
    }

Just as a side note. The program works when using my credentials, when going through my own server, just doesn't work when linking it to theirs.

1
When they use this program, do they use their own credentials (or do they use your credentials)? - Halvard
Also, have you seen this question: stackoverflow.com/questions/3155242/… - Halvard
I've been using the program with 2 sets of Hardcoded credentials: theirs and mine... They use the one with theirs. Yes I've looked at that answer, and no use as I said in the question, the Username and From are exactly the same, see MailConst.cs - Zach Ross-Clyne
I saw your edit - good catch! Do you have any more details on the Failure sending mail? Often this means that some setup related to the smtp server is preventing your email from being sent. Good luck finding it! - Halvard
Yeah its working now thanks for your support :) To use exchange you need to use a Domain to check the credentials, so I added a domain to it - Zach Ross-Clyne

1 Answers

9
votes

In order to fix this problem I had to use the optional parameter Domain for the NetworkCredential

My code now looks like this:

NetworkCredential basicCredential = new NetworkCredential(MailConst.UserName, MailConst.Password, MailConst.Domain

Where the MailConst.Domain is a string pointing to the Exchange domain.