Call Server Side Function On HTML Button Click

Viewed 1296

I have one button when i click on it... first my DIV gets execute/loaded which is working fine for me.

Now parallel i want to send an email to my account how i can call my server side function. Means when Admin click on "Mail Account Detail" i want to pop up DIV as well as Call my Server Side function names "SendAnEmail".

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#patPortalCredDiv">Mail Account Detail</button>

Here is my Div Code.

 <div id="patPortalCredDiv" class="modal fade" role="dialog">
 <div class="modal-dialog">
 <!-- Modal content-->
 <div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title" style="color: black;">Mail Credential</h4>
  </div>
  <div class="modal-body">
    <div class="container-fluid">
      <div class="row">
        <div class="form-group">
          <h4 for="UserName">Note:-
           <asp:Label type="text" runat="server" Text="" ID="txtMail"></asp:Label>
          </h4>

        </div>
      </div>
 </div>
 </div>
 <div class="modal-footer">
 <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Here is my server side function which i want to call also.

protected void SendAndEmail(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Server Side Code Also executed.');", true);

        }
2 Answers

Problem in this case were data-toggle and data-target attributtes preventing page to post back.

In your aspx page replace your button for this:

<asp:Button Text="Mail Account Detail" OnClick="SendAndEmail" 
    CssClass="btn btn-primary" type="button" runat="server" />

In your C# code behind

protected void SendAndEmail(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(this.GetType(), "alert", 
        "$('#patPortalCredDiv').modal('show');", true);

}

This will open bootstrap modal using jQuery

This is what I have done. I am using HTML and Java for the back-end to send the mail.

<body>
    <h3> Please Enter your Email Address </h3>
    <form action="Sendmail" method="post">
    <input type="text" class="form-control" placeholder="staff_email"  email="staff_email">
    <input type="submit" value="Submit">
    </form>
 </body>

On the back end side, I am using Gmail service.

public class EmailUtility{
public static void sendEmail(String host, String port,
        final String senderEmail, String senderName, final String password,
        String recipientEmail, String subject, String message) throws AddressException,
        MessagingException, UnsupportedEncodingException {

    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(senderEmail, password);
        }
    };

    Session session = Session.getInstance(properties, auth);

    //creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(senderEmail));
    InternetAddress[] toAddresses = { new InternetAddress(recipientEmail) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(message);

    //sends the e-mail
    Transport.send(msg);
    }
}

I am using Tomcat Server. Please add the following lines in the web.xml of Tomcat Server.

<context-param>
<param-name>email</param-name>
<param-value>abc@gmail.com</param-value>
</context-param>
<context-param>
<param-name>name</param-name>
<param-value><Your Name></param-value>
</context-param>
<context-param>
<param-name>pass</param-name>
<param-value> Your Password Comes Here </param-value>
</context-param>
Related