locked
how to send email by all SMTP address RRS feed

  • Question

  • User-1634604574 posted

    i have this code to send email but by these code i can just send email by gmail i cannot send email by yahoo or hotmail or any other types of email

    my question i how can i send email (i mean sender email) by all types of email

    controller

    public JsonResult SendMailToUser()
            {
                bool result = false;
                result= SendEmail2("zhyanadil.it@gmail.com", "ise","<p>hellooooo</p>");
                return Json(result, JsonRequestBehavior.AllowGet);
            }
    
            public bool SendEmail2(string toEmail,string subject,string emailBody)
            {
                try
                { 
                    string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString();
                    string senderpassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();
    
                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                    client.EnableSsl = true;
                    client.Timeout = 100000;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    client.Credentials =new NetworkCredential(senderEmail, senderpassword);
    
                    MailMessage mailmessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
                    mailmessage.IsBodyHtml = true;
                    mailmessage.BodyEncoding = UTF8Encoding.UTF8;
    
                    client.Send(mailmessage);
    
                    return true;
    
                }
    
                catch(Exception ex) {
    
                    return false;
                }
            }
    
    
            public ActionResult SendEmail()
            {
                return View();
            }

    view

    <a class="btn btn-danger" onclick="SendEmail()">click to send email</a>
    
    
    <p>hellooooo</p>
    
    
    <script>
    
        var SendEmail = function () {
    
            $.ajax({
                type: "POST",
                url: "/Main/SendMailToUser",
                success: function (data) {
                    alert("success");
                }
            })
        }
    
    </script>

    web.config

     <add key="SenderEmail" value="chnar.ise.1992@gmail.com" />
        <add key="SenderPassword" value="***" />

    Thursday, June 18, 2020 7:16 AM

All replies

  • User-821857111 posted

    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

    Asssuming that Yahoo, Hotmail etc permit the use of their SMTP servers for this, all you have to do is to change that to the settings that your other mail providers give you. For example. it used to be that hotmail/outlook's smtp address was smtp-mail.outlook.com (https://www.mikesdotnetting.com/article/268/how-to-send-email-in-asp-net-mvc) but that might have changed since I wrote that article. 

    Thursday, June 18, 2020 9:12 AM
  • User-474980206 posted

    You can send email via any email service that supports the smtp protocol. You just go to their site and get the smtp settings. You will an account on each service to send email from that service.

    Thursday, June 18, 2020 2:30 PM
  • User-1634604574 posted

    can you explain it more clearly by code?

    Friday, June 19, 2020 8:44 AM
  • User-1634604574 posted

     i mean by this code

    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

    i can just send email by gmail i want to write code general used for all types of emails like gmail yahoo hotmail and so on use tem as a sender email without enable less security

    Friday, June 19, 2020 11:27 AM
  • User475983607 posted

    zhyanadil.it@gmail.com

    i can just send email by gmail i want to write code general used for all types of emails like gmail yahoo hotmail and so on use tem as a sender email without enable less security

    A different configuration is required for each SMTP service.  Likewise, SMTP security is at the discretion of the SMTP service.   Basically, it is up to you to read the documentation for the SMTP service you wish to use and set "enable less security" if applicable.  

    I recommend purchasing a SMTP service from a hosting provider that you control.  

    Friday, June 19, 2020 11:42 AM
  • User753101303 posted

    Hi,

    A bit unclear. If you mean using whatever mail address you want for the sender this is  not possible for safety reasons. Usually it is the actual mail address for the actual account that sent the mail or inside a company it may be perhaps any address ending with your own domain name.

    Else you usually pick a mail provider and stick with it. Here it's unclear if it's a general technical question in case you would like to use another mail provider or if you try to pretend your mail message comes from a mailbox or domain name you don't own.

    Friday, June 19, 2020 1:35 PM
  • User-1634604574 posted

    if i don't enable less security why the email not sending?

    Friday, June 19, 2020 3:28 PM
  • User753101303 posted

    And so the current situation is ?

    If you mean you want to be able to use gmail without enabling https://hotter.io/docs/email-accounts/secure-app-gmail/ you'll have AFAIK to use another mail library such as  https://github.com/jstedfast/MailKit which is the preferred replacement for ASP.NET Core (you can use this from ASP.NET 4.x as well)

    If you get false, you should look ,or log exception details to understand what happens.

    If you mean you want to use any address you want for the mail sender this is AFAIK just not supposed to be possible.

    Friday, June 19, 2020 3:42 PM
  • User-474980206 posted

     i mean by this code

    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

    i can just send email by gmail i want to write code general used for all types of emails like gmail yahoo hotmail and so on use tem as a sender email without enable less security

    because smtpclient is old and does not support TSL. You must use the SSL port

    Friday, June 19, 2020 6:22 PM