4

i'm try to send some simple email to for example test@blabla.com

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 = "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 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 test@blabla.com (this happens when i leave the E-mail address textbox empty, this also happens when i've entering any email format such as sample@email.com / my primary e-mail)

Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49
user3848402
  • 193
  • 2
  • 3
  • 15
  • that's usually caused by incorrect login details. is there a user specified for the smtp? – user1666620 Aug 28 '14 at 08: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.. – user3848402 Aug 28 '14 at 08:56
  • Yeah, sometimes a domain user needs to be specified depending on the settings. Also take a look at this: http://stackoverflow.com/questions/3165721/mailbox-unavailable-the-server-response-was-5-7-1-unable-to-relay-for-abcxyz – user1666620 Aug 28 '14 at 09:00
  • Erm, you are setting test@blabla.com 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! – Paul Zahra Aug 28 '14 at 09:12
  • @PaulZahra:the test@blabla.com just a sample here, i've actually send it to my secondary email that exist.. – user3848402 Aug 28 '14 at 09:29
  • @user1666620: i've read your reference and set my virtual smtp server using IIS 6.0, but it return "Failure sending mail" – user3848402 Aug 28 '14 at 09:31

2 Answers2

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

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • 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 – user3848402 Aug 28 '14 at 09:34
  • But i thought you said you don't have the details for your smtp server? – Paul Zahra Aug 28 '14 at 10:15
  • 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 Aug 29 '14 at 06:28
  • @user3848402 Basically... you need a gmail account to send from... https://accounts.google.com/SignUp?service=mail&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fpc%3Dtopnav-about-en – Paul Zahra Aug 29 '14 at 08:08
  • 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 – user3848402 Sep 01 '14 at 04:52
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("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);
}

Just use async/await if you want wait the email sent.

andrefadila
  • 647
  • 2
  • 9
  • 36