Reputation: 193
i'm try to send some simple email to for example [email protected]
First of all, I've already tried it in my Localhost, and it worked, the problem was when i uploaded it to my Server
the Server i'm using is windows server 2012 R2, with IIS 7 (not really sure with the version but i believe it's 7 and above) hosted successfully with no problem, just the send email method don't work...
I've already add SMTP feature, set the SMTP E-MAIL(Yes, there are no SMTP Virtual server in IIS 7 and above)
here's my controller
public ActionResult SendTestEmail()
{
var test_address = "[email protected]";
try
{
// Initialize WebMail helper
WebMail.SmtpServer = "localhost";
WebMail.SmtpPort = 25; // Or the port you've been told to use
WebMail.EnableSsl = false;
WebMail.UserName = "";
WebMail.Password = "";
WebMail.From = "[email protected]"; // random email
WebMail.Send(to: test_address,
subject: "Test email message",
body: "This is a debug email message"
);
}
catch (Exception ex)
{
ViewBag.errorMessage = ex.Message; // catch error
}
return View();
}
here is my SMTP E-mail settings:
the error message are: Mailbox unavailable. The server response was: 5.7.1 Unable to relay for [email protected] (this happens when i leave the E-mail address textbox empty, this also happens when i've entering any email format such as [email protected] / my primary e-mail)
Upvotes: 4
Views: 13130
Reputation: 9725
You need to set your smtp settings to a real smtp server....
Try some smtp servers off this list if you don't have access to your own...
Here's a nice bit of code for you... Taken from here
MailModel.cs
public class MailModel
{
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
SendMailerController.cs - Notice where the smtp settings are stated
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
namespace SendMail.Controllers
{
public class SendMailerController : Controller
{
// GET: /SendMailer/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ViewResult Index(SendMail.Models.MailModel _objModelMail)
{
if (ModelState.IsValid)
{
MailMessage mail = new MailMessage();
mail.To.Add(_objModelMail.To);
mail.From = new MailAddress(_objModelMail.From);
mail.Subject = _objModelMail.Subject;
string Body = _objModelMail.Body;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("username", "password");// Enter senders User name and password
smtp.EnableSsl = false;
smtp.Send(mail);
return View("Index", _objModelMail);
}
else
{
return View();
}
}
}
}
Index.cshtml
@model SendMail.Models.MailModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<fieldset>
<legend>Send Email</legend>
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<p>From: </p>
<p>@Html.TextBoxFor(m=>m.From)</p>
<p>To: </p>
<p>@Html.TextBoxFor(m=>m.To)</p>
<p>Subject: </p>
<p>@Html.TextBoxFor(m=>m.Subject)</p>
<p>Body: </p>
<p>@Html.TextAreaFor(m=>m.Body)</p>
<input type ="submit" value ="Send" />
}
</fieldset>
UPDATE How to setup SMTP server on IIS7
start->administrative tools->server manager, go to features, select "add features", tick "smtp server" (if it is not already installed), choose to install the required "remote server admin toos"
check to confirm that "Simple Mail Transfer Protocol (SMTP)" service is running, if so, we are good to go.
start->administrative tools>internet info services(iis) 6.0
make sure that SMTP virtual server/default smtp server is running, if not, right click, then choose "start"
in IIS7, go to website/virtual directory, double click "SMTP E-mail", Click on "Deliver e-mail to SMTP server", check the "Use localhost" checkmark
Upvotes: 4
Reputation: 687
Why not try to use gmail for email sender? It's easy to use.
I always just build simple method
public void SendEmail()
{
MailMessage mail = new MailMessage("[email protected]", "sendTo", "mailSubject", "mailBody");
mail.From = new MailAddress("[email protected]", "nameEmail");
mail.IsBodyHtml = true; // necessary if you're using html email
NetworkCredential credential = new NetworkCredential("[email protected]", "xxxxx");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = credential;
smtp.Send(mail);
}
Just use async/await
if you want wait the email sent.
Upvotes: 1