Send Simple Email ASP.NET MVC, IIS7

I am trying to send a simple email, for example, to test@blabla.com

First of all, I already tried this in my Localhost and it worked, the problem was when I uploaded it to my server

The server I use is a Windows 2012 R2 server, with IIS 7 (not quite sure of the version, but I believe that 7 and above) succeeded without problems, just the method of sending email does not work ...

I already have SMTP function, install SMTP E-MAIL (Yes, in IIS 7 and above there is no virtual SMTP server)

here is my controller

public ActionResult SendTestEmail()
{
    var test_address = "test@blabla.com";
    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 = "admin@testserver.com"; // 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 are my SMTP email settings: enter image description here

: . : 5.7.1 test@blabla.com( , , , , sample@email.com/ )

+4
2

smtp SMTP-....

smtp- , ...

...

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 - , smtp

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 SMTP- IIS7

  • start- > Administrative tools- > server manager, , " ", "smtp-" ( ), " " "

  • , "Simple Mail Transfer Protocol (SMTP)", , .

  • start- > > - (iis) 6.0

  • , SMTP- / smtp , , , "start"

  • IIS7 -/ , "SMTP E-mail", " SMTP-", " "

+3

gmail ? .

public void SendEmail()
{
    MailMessage mail = new MailMessage("xxx@gmail.com", "sendTo", "mailSubject", "mailBody");
    mail.From = new MailAddress("xxx@gmail.com", "nameEmail");
    mail.IsBodyHtml = true; // necessary if you're using html email

    NetworkCredential credential = new NetworkCredential("xxx@gmail.com", "xxxxx");
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = credential;
    smtp.Send(mail);
}

async/await, .

+1

All Articles