Reputation: 1855
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
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
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
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