2

I am trying to setup simple but complete ASP.NET MVC 4 web app, where I can send email to specific address, I configure the web.config file for SMPT settings and code in controller call, but I am getting error message "The SMTP host was not specified"

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="myEmail@hotmail.co.uk">
    <network host="smtp.live.com" port="25" userName="myEmail@hotmail.co.uk" password="myPassword" defaultCredentials="true"/>
  </smtp>
</mailSettings>

in controller class

var mailMessage = new MailMessage();
mailMessage.To.Add("yourEmail@hotmail.co.uk");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;

var smptClient = new SmtpClient { EnableSsl = false };

smptClient.Send(mailMessage);

many thanks

K.Z
  • 5,201
  • 25
  • 104
  • 240

7 Answers7

2

Best Idea to use SMTP mail functionality in .NET + MVC/ASP is this open source codeplex library:

http://higlabo.codeplex.com/

Especially since the default-delivered components in .NET framework does fully support all types of SSL/TSL etc. (implicit/explicit mode as keyword here)

http://higlabo.codeplex.com/

johngrinder
  • 422
  • 4
  • 14
1

From a quick look you haven't set up the "From" property.

var mailMessage = new MailMessage();
mailMessage.To.Add("yourEmail@hotmail.co.uk");
mailMessage.From = new MailAddress("myEmail@hotmail.co.uk");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
AthibaN
  • 2,087
  • 1
  • 15
  • 22
Jack Hughes
  • 5,514
  • 4
  • 27
  • 32
0

do some thing like this

SmtpClient smtp = new  SmtpClient(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtp], Convert.ToInt32(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpport]));

if (ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpUseCredentials] == "true")
{
    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpusername], ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtppassword], ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtp]);
}
else
{
    smtp.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    if (SendTo.Count == 0)
    {
        SendTo.Add(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.ToMail]);
    }

    foreach (string recipientemail in SendTo)
    {
        oEmail.To.Add(recipientemail);
        try
        {
            smtp.Send(oEmail);
        }
        catch (Exception)
        {
        }
        oEmail.To.Clear();
    }
}
Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
ali
  • 182
  • 2
  • 2
  • 10
0

Your code and configuration look correct.

Are you sure you have put the system.net/mailSettings element in the web.config in the root directory of your web site?

A common mistake is to put such settings in the web.config in the Views folder.

Incidentally, the MailMessage class implements IDisposable, as does the SmtpClient class from .NET 4. So you should be enclosing both in using blocks.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • I have added smpt configuration in root web.config not view one! do i have to put setting in web.config??? – K.Z Sep 24 '13 at 10:55
0
pateketu
  • 451
  • 2
  • 8
0

I wrote a blog post about doing this. http://www.bgsoftfactory.net/5-steps-to-send-email-with-mvcmailer-from-an-mvc-4-0-application/

I took the easier way, using MVCMailer. Even if sending email from MVC is quite easy, it's a little more complicated to make it nice, while MVCMailer allow to use razor templates to format the body of your email.

You may save yourself some time by using MVCMailer.

jnm2
  • 7,960
  • 5
  • 61
  • 99
BernardG
  • 1,956
  • 3
  • 17
  • 25
0

you missing smptClient.Send(mailMessage); at the end of your code

var mailMessage = new MailMessage();
mailMessage.To.Add("yourEmail@hotmail.co.uk");
mailMessage.From = new MailAddress("myEmail@hotmail.co.uk");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
//this what you miss
    smptClient.Send(mailMessage);
//