locked
send list of email by button click RRS feed

  • Question

  • User-1634604574 posted

    i have this table i want by button click send message inside column2 into list of email in column1

    <table id="tb1">
    <tr><th>email</th> <th>messages</th></tr>
    <tr><td>email1@gmail.com</td><td>hello email 1</td></tr>
    <tr><td>email2@ymail.com</td><td>hello email 2</td></tr>
    <tr><td>email3@hotmail.com</td><td>hello email 3</td></tr>
    <tr><td>email4@gmail.com</td><td>hello email 4</td></tr>
    
    
    </table>

    how can i do it?

    Monday, June 1, 2020 7:37 PM

Answers

  • User475983607 posted

    i don't have any exception, and i don't know how can i  looking for any security warning in my gmail account

    I tested your code and it works.  However, I had to enable "Less Secure Applications" in my Google account setting.  

    You'll need to contact Google support for assistance if you do not know how to login to your Google account.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 2:32 PM
  • User475983607 posted

    how to enable Less Secure Applications?

    Login to your Google account and click Security.

    https://myaccount.google.com/security

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 6:16 PM
  • User1686398519 posted

    Hi,  zhyanadil

    To resolve this error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.",you can make the following settings:

    • Turn on the Access for less secure apps.Here is the link.

    • You will receive an email in your Google mailbox, and then follow the email instructions.

    In addition, I modified the code according to your needs, please refer to it.

    Controller

            [HttpPost]
            public JsonResult SendMailToUser(Array receiverEmails, Array emailBodys)
            {
                bool result = false;
    
                result = SendEmail2(receiverEmails, "ise", emailBodys);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
    
            public bool SendEmail2(Array receiverEmails,string subject,Array 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);
                  for (int i = 0; i < receiverEmails.Length; i++)
    {
    MailMessage mailmessage = new MailMessage(senderEmail, receiverEmails.GetValue(i).ToString(), subject, emailBody.GetValue(i).ToString());
    mailmessage.IsBodyHtml = true;
    mailmessage.BodyEncoding = UTF8Encoding.UTF8;
    client.Send(mailmessage);
    } return true; } catch (Exception ex) { return false; } }

    Page

    <script>
        var SendEmail = function () {
            var receiverArray = new Array();
            var emailBodyArray = new Array();
            $('#tb1 tr').each(function (i) {
                $(this).children('td').each(function (j) {
                    if (j % 2 != 0) {
                        emailBodyArray.push($(this).text());
                    } else {
                        receiverArray.push($(this).text());
                    }
                });
            });
            $.ajax({
                type: "POST",
                url: "/Home/SendMailToUser",
                data: { receiverEmails: receiverArray, emailBodys: emailBodyArray},
                success: function (data) {
                    alert("success");
                }
            })
        }
    </script>

    Here is the result.

     
    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 3, 2020 4:00 AM

All replies

  • User1686398519 posted

    Hi,  zhyanadil

    I want to confirm some questions with you:

    First of all, can you speak more specifically about the phrase "send message inside column2 into list of email in column1"?

    Second, do you need to pass the information  to the controller? Or does it need to be stored in the database? Do you need a model for your email?

    Best Regards,

    YihuiSun

    Tuesday, June 2, 2020 1:16 AM
  • User-1634604574 posted

    first i mean 

    send hello email 1 to this email (email1@gmail.com)
    hello email 2  to this email (email2@ymail.com)
    hello email 3 to this email (email3@hotmail.com)
    hello email 4 to this email (email4@gmail.com)

    send all of them by one click

    secondly i dont want ti send to controller to be insert into database
    i just want to send those messages to those emails by one click on button
    Tuesday, June 2, 2020 8:57 AM
  • User-1634604574 posted

    i used this to send one email just for test but it is not sending email

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

    web.config

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

    Tuesday, June 2, 2020 12:56 PM
  • User475983607 posted

    Your design hides exceptions which is a very poor design.  Set a break point and debug your code to find exceptions.    If there are no exceptions then login to your gmail account and look for any security warnings.  

    Tuesday, June 2, 2020 1:14 PM
  • User-1634604574 posted

    i don't have any exception, and i don't know how can i  looking for any security warning in my gmail account

    Tuesday, June 2, 2020 2:18 PM
  • User475983607 posted

    i don't have any exception, and i don't know how can i  looking for any security warning in my gmail account

    I tested your code and it works.  However, I had to enable "Less Secure Applications" in my Google account setting.  

    You'll need to contact Google support for assistance if you do not know how to login to your Google account.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 2:32 PM
  • User-1634604574 posted

    how to enable Less Secure Applications?

    Tuesday, June 2, 2020 4:43 PM
  • User475983607 posted

    how to enable Less Secure Applications?

    Login to your Google account and click Security.

    https://myaccount.google.com/security

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 6:16 PM
  • User1686398519 posted

    Hi,  zhyanadil

    To resolve this error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.",you can make the following settings:

    • Turn on the Access for less secure apps.Here is the link.

    • You will receive an email in your Google mailbox, and then follow the email instructions.

    In addition, I modified the code according to your needs, please refer to it.

    Controller

            [HttpPost]
            public JsonResult SendMailToUser(Array receiverEmails, Array emailBodys)
            {
                bool result = false;
    
                result = SendEmail2(receiverEmails, "ise", emailBodys);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
    
            public bool SendEmail2(Array receiverEmails,string subject,Array 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);
                  for (int i = 0; i < receiverEmails.Length; i++)
    {
    MailMessage mailmessage = new MailMessage(senderEmail, receiverEmails.GetValue(i).ToString(), subject, emailBody.GetValue(i).ToString());
    mailmessage.IsBodyHtml = true;
    mailmessage.BodyEncoding = UTF8Encoding.UTF8;
    client.Send(mailmessage);
    } return true; } catch (Exception ex) { return false; } }

    Page

    <script>
        var SendEmail = function () {
            var receiverArray = new Array();
            var emailBodyArray = new Array();
            $('#tb1 tr').each(function (i) {
                $(this).children('td').each(function (j) {
                    if (j % 2 != 0) {
                        emailBodyArray.push($(this).text());
                    } else {
                        receiverArray.push($(this).text());
                    }
                });
            });
            $.ajax({
                type: "POST",
                url: "/Home/SendMailToUser",
                data: { receiverEmails: receiverArray, emailBodys: emailBodyArray},
                success: function (data) {
                    alert("success");
                }
            })
        }
    </script>

    Here is the result.

     
    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 3, 2020 4:00 AM