Reputation: 5075
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="[email protected]">
<network host="smtp.live.com" port="25" userName="[email protected]" password="myPassword" defaultCredentials="true"/>
</smtp>
</mailSettings>
var mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
var smptClient = new SmtpClient { EnableSsl = false };
smptClient.Send(mailMessage);
many thanks
Upvotes: 2
Views: 20540
Reputation: 1966
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.
Upvotes: 0
Reputation: 182
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();
}
}
Upvotes: 0
Reputation: 1
you missing smptClient.Send(mailMessage); at the end of your code
var mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
//this what you miss
smptClient.Send(mailMessage);
//
Upvotes: 0
Reputation: 422
Best Idea to use SMTP mail functionality in .NET + MVC/ASP is this open source codeplex library:
Especially since the default-delivered components in .NET framework does fully support all types of SSL/TSL etc. (implicit/explicit mode as keyword here)
Upvotes: 2
Reputation: 451
Upvotes: 0
Reputation: 124686
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.
Upvotes: 0
Reputation: 5654
From a quick look you haven't set up the "From" property.
var mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
Upvotes: 1