James Radford
James Radford

Reputation: 1855

sending local emails to hotmail issue

I have the following code that causes 'The SMTP host was not specified.' Any ideas why this happen? Many thanks

var mailMessage = new System.Net.Mail.MailMessage();
mailMessage.To.Add(new MailAddress("[email protected]"));
mailMessage.From = new MailAddress("[email protected]");

mailMessage.Subject = "my test subject";
mailMessage.Body = "my test body";
mailMessage.IsBodyHtml = true;

var smtpClient = new SmtpClient { EnableSsl = true };
object userState = mailMessage;
smtpClient.Send(mailMessage);

I've tried the following now and it still fails

            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("[email protected]", "password"),
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false
            };

            var mail = new MailMessage("[email protected]", "[email protected]", "hello", "there");
            mail.Body = "Hello";
            mail.Subject = "hi";
            client.Send(mail);

Upvotes: 2

Views: 1349

Answers (5)

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

var client = new SmtpClient(smtpServer, 25)
{
    Credentials = new NetworkCredential(userName, password),
    EnableSsl = false
};

MailMessage mail = new MailMessage(sender, receiver, head, body);
client.Send(mail);

You should specify your Smtp Server as shown above.


Or you can specify it at web.config file.

<mailSettings>
  <smtp>
    <network 
       host="server" 
       port="portNumber"
       userName="username"
       password="password" />
  </smtp>
</mailSettings>

Upvotes: 1

Mubarek
Mubarek

Reputation: 2689

Enable SSL before you send the message

smtpClient.EnableSsl=true;

Upvotes: 0

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9942

Did you add mailSettings in web.config? Please check the below link by Scott.

http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx

Upvotes: 0

RubbleFord
RubbleFord

Reputation: 7636

You don't appear to have defined a server to send through, unless you have done this in your application config.

<system.net>
        <mailSettings>
            <smtp>
                <network host="127.0.0.1" port="25"/>
            </smtp>
        </mailSettings>
    </system.net>

You need to specify your settings rather than the local ones I used in the example above.

Upvotes: 1

Jared Shaver
Jared Shaver

Reputation: 1339

Because you have not specified the smtpClient.Host property.

Upvotes: 2

Related Questions