locked
Email RRS feed

  • Question

  • User-1952405402 posted

    I'm developing ASP.Net Core Application, I'm trying here to Send email with some information to my email but is not work

    I followed some tutorial where i created the following : 

    -View: 

    @model Email_sending.Models.Report

    @{
        ViewData["Title"] = "Index";
    }


    <h1> index </h1>
    <h4>Email</h4>
    <hr />
    <div class="row">
    <div class="col-md-4">
        <from asp-action="Index">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="from-group">
                <label asp-for="To" class="control-label"></label>
                <input asp-for="To" class="from-control" />
                <span asp-validation-for="To" class="text-danger"></span>
            </div>

            <dive class="from-group">
                <label asp-for="Subject" class="control-label"></label>
                <input asp-for="Subject" class=" from-control" />
                <span asp-validation-for="Subject" class="text-danger"></span>
            </dive>

            <dive class="from-group">
                <label asp-for="Body" class="control-label"></label>
                <textarea asp-for="Body" class=" from-control" rows="3" cols="15" />
                <span asp-validation-for="Body" class="text-danger"></span>
            </dive>

             <dive class="from-group">
                 <input type="submit" value="Send Email" class="btn btn-primary" />
                 </dive>
            <h5>@ViewBag.message</h5>
        </from>

    </div>
    </div>

    -Model : 

    using System;
    namespace Email_sending.Models
    {
        public class Report
        {
            public string To { get; set; }
            // public string Type { get; set; }
            public string Subject { get; set; }
            public string Body { get; set; }

            public enum problemTyep {NA=0,problemone=1,problemtwo=2,problemthree=3}
        }

        
    }

    -controller :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Email_sending.Models;
    using System.Net.Mail;

    namespace Email_sending.Controllers
    {
        public class SendMailController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }

            [HttpPost]
            public IActionResult Index(Report re)
            {
                string to = re.To;
                string subject = re.Subject;
                string body = re.Body;
                MailMessage mm = new MailMessage();
                mm.To.Add(to);
                mm.Subject = subject;
                mm.Body = body;
                mm.From = new MailAddress("Wejdan_011@hotmail.com");
                mm.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                smtp.Port = 587;
                smtp.UseDefaultCredentials = true;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential("Wejdan_011@hotmail.com" ,"Password"  );
                smtp.Send(mm);
                ViewBag.message = "The mail has been send to" + re.To + "Successfully..";


                return View();
            }
        }

     

    Sunday, September 20, 2020 2:56 PM

All replies

  • User475983607 posted

    What does "not working" mean?  Did you receive an error?  Should we assume you have a gmail login as hotmail is not going to work.   Did the code execute but the email did not arrive?  Have you tried checking your gmail account for any security warnings?  Did you enable less secure applications in your gmail account?

    https://support.google.com/accounts/answer/6010255?hl=en

    What troubleshooting steps have you performed?

    Sunday, September 20, 2020 3:23 PM
  • User-474980206 posted

    You are using a hotmail account with a gmail server. They need match. Either use a hotmail server, or gmail account.

    Sunday, September 20, 2020 3:25 PM
  • User-1952405402 posted

    I mean when i click submit , and then i go to check my mail  i didn't receive the mail 

    Sunday, September 20, 2020 6:44 PM
  • User475983607 posted

    I mean when i click submit , and then i go to check my mail  i didn't receive the mail 

    Use the Visual Studio debugger to troubleshoot your code.  https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Be aware that sending email is very common and the SmtpClient works fine.  Usually problems are misconfiguration and security.  Configuration comes form the SMTP service you are trying to use.  You must read the documentation for the SMTP service you are using.   Security requires you login to your SMTP service and set any necessary configuration.  Gmail has "enable less secure apps".

    Sunday, September 20, 2020 7:21 PM