4

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: enter image description here

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)

6
  • that's usually caused by incorrect login details. is there a user specified for the smtp? Commented Aug 28, 2014 at 8:52
  • hmmm... do you mean the username and password i use to access the server?? if that the case i have, but for the smtp i think i don't have one.. Commented Aug 28, 2014 at 8:56
  • Yeah, sometimes a domain user needs to be specified depending on the settings. Also take a look at this: stackoverflow.com/questions/3165721/… Commented Aug 28, 2014 at 9:00
  • Erm, you are setting [email protected] when you do WebMail.Send(to: test_address so don't be suprised it allways tries to send to it... and guess what... it doesn't exist! Commented Aug 28, 2014 at 9:12
  • @PaulZahra:the [email protected] just a sample here, i've actually send it to my secondary email that exist.. Commented Aug 28, 2014 at 9:29

2 Answers 2

4

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

  1. 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"

  2. check to confirm that "Simple Mail Transfer Protocol (SMTP)" service is running, if so, we are good to go.

  3. start->administrative tools>internet info services(iis) 6.0

  4. make sure that SMTP virtual server/default smtp server is running, if not, right click, then choose "start"

  5. 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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank for your answer very appreciated, i've tried those method and it works, but it use gmail smtp, and i want to use my server smtp... I don't know if my server can actually have smtp server or not though... anyway thx :D
But i thought you said you don't have the details for your smtp server?
sorry for the late reply, after discussing with my manager, he's agree to using gmail smtp, but for temporary use... i use your code, follow all the step but, the exception return: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at...
@user3848402 Basically... you need a gmail account to send from... accounts.google.com/…
sorry, cause weekend i've ended up take a vacation. I've tried your method, i think the problem lies in my server, i've tried my company smtp, it actually send the email but quite a long time, about 2 hour... Thx Paul, i've use your method :D
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.