• This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts

  • kevin
    Keymaster
    Postcount 41

    Please guide me how setup and send email in ASP.NET Core?


    kevin
    Keymaster
    Postcount 41

    You can do as below:

    1. Add configurations to appsettings.json:

    {
    “Gmail”: {
    “Host”: “smtp.gmail.com”,
    “Port”: 587,
    “Username”: “YOUR GMAIL”,
    “Password”: “PASSWORD OF GMAIL”,
    “SMTP”: {
    “starttls”: {
    “enable”: true
    }
    }
    }
    }

    2. Create MailHelper:

    using Microsoft.Extensions.Configuration;
    using System;
    using System.Net;
    using System.Net.Mail;

    namespace MyProject.Helpers
    {
    public class MailHelper
    {
    private IConfiguration configuration;

    public MailHelper(IConfiguration _configuration)
    {
    this.configuration = _configuration;
    }

    public bool Send(string from, string to, string subject, string content)
    {
    try
    {
    var host = configuration[“Gmail:Host”];
    var port = int.Parse(configuration[“Gmail:Port”]);
    var username = configuration[“Gmail:Username”];
    var password = configuration[“Gmail:Password”];
    var enable = bool.Parse(configuration[“Gmail:SMTP:starttls:enable”]);

    var smtpClient = new SmtpClient
    {
    Host = host,
    Port = port,
    EnableSsl = enable,
    Credentials = new NetworkCredential(username, password)
    };

    var mailMessage = new MailMessage(from, to);
    mailMessage.Subject = subject;
    mailMessage.Body = content;
    mailMessage.IsBodyHtml = true;

    mailMessage.Attachments.Add(new Attachment(“~/images/thumb2.gif”));

    smtpClient.Send(mailMessage);

    return true;
    }
    catch(Exception e2)
    {
    return false;
    }
    }
    }
    }

    3. Send Email in Controller:

    using MyProject.Helpers;
    using MyProject.Models;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using System;

    namespace MyProject.Controllers
    {
    [Route(“contact”)]
    public class ContactController : Controller
    {
    private IConfiguration configuration;

    public ContactController(IConfiguration _configuration)
    {
    configuration = _configuration;
    }

    [Route(“”)]
    [Route(“index”)]
    [Route(“~/”)]
    public IActionResult Index()
    {
    return View(“Index”, new Contact());
    }

    [HttpPost]
    [Route(“send”)]
    public IActionResult Send(Contact contact)
    {
    try
    {
    var body = “Name: ” + contact.Name + “<br>Address: ” + contact.Address + “<br>Phone: ” + contact.Phone + “<br>” + contact.Content;
    var mailHelper = new MailHelper(configuration);
    if (mailHelper.Send(contact.Email, configuration[“Gmail:Username”], contact.Subject, body))
    {
    return View(“Success”);
    }
    else
    {
    ViewBag.err = “Failed”;
    return View(“Index”);
    }
    }
    catch(Exception e)
    {
    ViewBag.err = “Failed”;
    return View(“Index”);
    }
    }

    }
    }

     

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Top