locked
how to use business email RRS feed

  • Question

  • User-1634604574 posted

    i want to send email by using business email as a sender i wrote this code but is not working

    here is my sender email which is business email (company email)  mytech@info.com

    view  SendEmail

    <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>

    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.info.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();
            }

    web.config

      <add key="SenderEmail" value="mytech@info.com" />
        <add key="SenderPassword" value="***" />

    Thursday, July 23, 2020 5:17 PM

All replies

  • User475983607 posted

    Your code hides exceptions.   

            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.info.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;
                }
            }

    I recommend removing the try...catch or writing a proper catch handler.  I also recommend that you run your code through the Visual Studio debugger.  

    Thursday, July 23, 2020 5:31 PM
  • User-1634604574 posted

    are you checked that example on your side by using company email? and i don't have enable less secure in that company email is it possible?

    Thursday, July 23, 2020 5:46 PM
  • User475983607 posted

    are you checked that example on your side by using company email?

    I can't test your code on with my company email.  It's a bit odd that you think it's possible as if all email servers are the same.  Ridiculous. 

    and i don't have enable less secure in that company email is it possible?

    I know that gmail has this setting but I have no idea how your company email is setup.  It's a bit odd that you don't know how your company email works.

    Dude, fix your poorly written code so you stop hiding errors from yourself.  Run your code through the debugger.  If the code runs without error then check the destination email account for spam.

    Thursday, July 23, 2020 6:03 PM
  • User-1634604574 posted

    how can i check the destination email account for smap

    Thursday, July 23, 2020 6:24 PM
  • User475983607 posted

    how can i check the destination email account for smap

    You seriously do not know how to check your email and look for spam?  It's common to have a spam folder but I can't help.  Perhaps there is someone that can help you with your company email if you do not know how it works?

    Thursday, July 23, 2020 6:48 PM
  • User-1634604574 posted

    yes i don't know how company email work

    Thursday, July 23, 2020 7:05 PM
  • User-474980206 posted

    As we don’t know how your smtp server was configured, we can not help. Ask whoever is in charge of the email server for connection information.

    Thursday, July 23, 2020 7:12 PM
  • User1686398519 posted

    Hi zhyanadil.it,

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

    I think you need to obtain the server settings from the email provider such as stmp host name, port. Because the port number of mytech@info.com you use may not be 587.

    If you can't contact the email provider, you can try these ways to get your email server's IMAP and SMTP information.You can also find the server settings in Outlook 2016.

    Best Regards,

    YihuiSun

    Friday, July 24, 2020 2:59 AM
  • User753101303 posted

    Hi,

    Define "not working". If you mean the mail you are sending is showing the gmail address for this account rather than mytech@info.com this is AFAIK expected.

    If this is a real address you own (but then you could use this mail server rather than going through gmail?) try https://support.google.com/mail/answer/22370?hl=en

    In  short for safety reasons (ie avoid forgery/phising) before using a sender mail address you must prove this mail address belongs to you.

    Sunday, July 26, 2020 11:40 AM