Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Afternoon,

I'm doing an app in which one of the modules includes sending email from the ASP.Net MVC app.

Here's what I already have :
C#
//This is email creation
               var body = "<p>Email From: {0} ({1})</p><p>Message: Morning. This is a test trying sending emails from ASP.Net MVC. Thanks to tell me if you receive the message. </p><p>{2}</p>";

               var message = new MailMessage();
               //message.To.Add(new MailAddress("eunicelakloe@yahoo.fr"));
               message.To.Add(new MailAddress("nice55kloe@mail.com"));
               message.From = new MailAddress(model.FromEmail);
               //message.From = new MailAddress("emapong@secelgroup.com");
               message.Subject = "This is a test trying sending emails with ASP.Net MVC";
               message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
               message.IsBodyHtml = true;

               //This manages the created email sending
               using (var smtp = new SmtpClient())
               {
                   var credential = new NetworkCredential
                   {
                       UserName = "myemail@gmail.com",  // replace with valid value
                       Password = "password"  // replace with valid value
                   };
                   smtp.Credentials = credential;
                   smtp.Host = "smtp-mail.outlook.com";
                   smtp.Port = 465;
                   smtp.EnableSsl = true;

                   await smtp.SendMailAsync(message);
                   return RedirectToAction("Sent");
               }
           }
           return View(model);


HTML form
HTML
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <h4>Send your comments.</h4>
    <hr />
    <div class="form-group">
        @Html.LabelFor(m => m.FromName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.FromName)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.FromEmail, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.FromEmail)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Message, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Message)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Send" />
        </div>
    </div>
}


What I expect is to get the email sent once I fill the form and click on the send button, but actually, once I click on the send button, the page loads forever without returning any result.I have no idea where the problem lies.

Any help will be greatly welcomed.
Thanks in advance!

What I have tried:

Adding breakpoints at some lines in controller, but nothing seems to get it going or even display some error.
Posted
Updated 16-Jul-19 6:26am
Comments
F-ES Sitecore 16-Jul-19 11:16am    
Run a local smtp server (google for "smtp4dev") to try your code basic against. If it works against that but not with your actual values then there is maybe a network issue between your server and the smtp server, or what you assume are "valid values" are not valid at all.
Nice Kloe 16-Jul-19 11:48am    
Thanks for your response F-ES Sitecore, but I'm sorry I do not understand what you mean when you say I should "Run a local smtp server (google for "smtp4dev") to try your code basic against."
F-ES Sitecore 16-Jul-19 11:53am    
There's a desktop application called smtp4dev that you can run on your server and it acts as an smtp server and will show you the details of any email it receives. When it is running, set the smtp server in your code to to 127.0.0.1 (or the address of the machine running smtp4dev if you can't run it on the web server) and see if the smtp4dev app receives the email ok. If it does then your code is ok, the issue is networks or settings. PROTIP though, don't send email through gmail, it will cause you nothing but problems, send it through the smtp server provided by your webhost\network admin.
Nice Kloe 18-Jul-19 3:56am    
As mentionned previously, I downloaded smtp4dev and installed, then tested my code and the code worked like a charm. I received the email. So, the problem should be my network or settings, right?
F-ES Sitecore 18-Jul-19 4:17am    
Yeah, either the server your code runs on can't physically connect to the smtp server you are using, or you have the ssl setting wrong, the port wrong, etc etc.

1 solution

The most common issues I have seen with gmail is

1. Google does not recognize the connection being used for the particular email account being used. The best thing to do is to go to their Login Activity page and tell them basically that "yes, that was me"
Sign in - Google Accounts[^]

2. Google by default blocks SMTP access under the premise that it is less secure. There is an option to allow "Less Secure Apps" which will allow SMTP access
Account settings: Your browser is not supported.[^]
 
Share this answer
 
Comments
Nice Kloe 16-Jul-19 13:38pm    
As per point 1, Google sent me a Security Alert email and I confirmed that the activity was from me
As per point 2, I just tried by allowing Less Secure Apps have access to my account, but I end to the same result : the ASP app loads forever.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900