Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:53:24+00:00 2026-05-26T00:53:24+00:00

I am trying to send Email from a Java program that I found here

  • 0

I am trying to send Email from a Java program that I found here http://stackoverflow.com/questions/73580/how-do-i-send-an-smtp-message-from-java , This code works fine(with the hardcoded information), but I want to send the information from an HTML page that I created, It has
From, To, Subject,Text inputs but I dont know how to send the values of these inputs from HTML page to this Java program.. Any help will be appreciated. Thanks

Here is my Form

<html>
<head>
<title>Send Email</title>

</head>
<body>
<h1 align="center">Email Sending Client</h1>
</tr></td>
<table width=30% align="center">

<form id=myform method="get" action="" onSubmit="return validate();">

<br>
<tr>
<td>
<b>From:</b>
</td>
<td>
<input type="text" name="email" id="email">
</td>
</tr>



<tr>
<td>
<b>To:</b>
</td>
<td>
<input type="text" name="to" id="to">
</td>
</tr>



<tr>
<td>
<b>Subject:</b>
</td>
<td>
<input type="text" name="sub" id="sub">
</td>
</tr>






<tr>
<td>
<b>Message:</b>
</td>
<td>
<p><textarea name="textarea" cols="16" rows="6"></textarea></p>
</td>
</tr>

<tr>
<td>
<br>
<input type=submit value="Send" align="center">
</td>
</tr>

</table>
</body>
</form>
</html>

Code for Servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import com.sun.mail.smtp.*;

public class sendemail extends HttpServlet {


 public static void main(String args[]) throws Exception ,ServletException, IOException{

      HttpServletRequest request=null;
      HttpServletResponse response=null;
    // Set response content type
      response.setContentType("text/html");

      PrintWriter out = response.getWriter();
      String title = "Using GET Method to Read Form Data";


     String email= request.getParameter("email");  
     String to= request.getParameter("to");  
     String sub= request.getParameter("sub");
     String smtp= request.getParameter("smtp");
     String mess= request.getParameter("mess"); 


            Properties props = System.getProperties();
            props.put("mail.smtps.host","smtp.gmail.com");
            props.put("mail.smtps.auth","true");
            Session session = Session.getInstance(props, null);
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(email));;
            msg.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(to, false));
            msg.setSubject(sub);

            msg.setText(mess);
            //msg.setHeader("X-Mailer", "Tov Are's program");
            msg.setSentDate(new Date());
            SMTPTransport t =
                (SMTPTransport)session.getTransport("smtp");
            t.connect(smtp, "mymail@gmail.com", "mypassword");
            t.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Response: " + t.getLastServerResponse());
            t.close();
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:53:25+00:00Added an answer on May 26, 2026 at 12:53 am
    <html>
    <head>
    <title>Send Email</title>
    
    </head>
    <body>
    <h1 align="center">Email Sending Client</h1>
    </tr></td>
    <table width=30% align="center">
    
    <form id=myform method="get" action="sendemail" onSubmit="return validate();">
    
    Your html form code
    
    </form>
    </html>
    

    sendemail.java inside com.zetcode package

    package com.zetcode;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    public class sendemail extends HttpServlet {
    
        protected void processRequest(HttpServletRequest request,
                                      HttpServletResponse response)
                       throws IOException, ServletException {
    
            final String err = "/error.jsp";
            final String succ = "/success.jsp";
    
            String from = request.getParameter("from");
            String to = request.getParameter("to");
            String subject = request.getParameter("subject");
            String message = request.getParameter("message");
    
            try {
                Properties props = new Properties();
                props.setProperty("mail.host", "smtp.gmail.com");
                props.setProperty("mail.smtp.port", "587");
                props.setProperty("mail.smtp.auth", "true");
                props.setProperty("mail.smtp.starttls.enable", "true");
                Authenticator auth = new SMTPAuthenticator("username","password");
                Session session = Session.getInstance(props, auth);
                MimeMessage msg = new MimeMessage(session);
                msg.setText(message);
                msg.setSubject(subject);
                msg.setFrom(new InternetAddress("name <"+from+">"));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                Transport.send(msg);
    
            } catch (AuthenticationFailedException ex) {
                request.setAttribute("ErrorMessage", "Authentication failed");
    
                RequestDispatcher dispatcher = request.getRequestDispatcher(err);
                dispatcher.forward(request, response);
    
            } catch (AddressException ex) {
                request.setAttribute("ErrorMessage", "Wrong email address");
    
                RequestDispatcher dispatcher = request.getRequestDispatcher(err);
                dispatcher.forward(request, response);
    
            } catch (MessagingException ex) {
                request.setAttribute("ErrorMessage", ex.getMessage());
    
                RequestDispatcher dispatcher = request.getRequestDispatcher(err);
                dispatcher.forward(request, response);
            }
                RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
                dispatcher.forward(request, response);
    
        }
    
        private class SMTPAuthenticator extends Authenticator {
    
            private PasswordAuthentication authentication;
    
            public SMTPAuthenticator(String login, String password) {
                authentication = new PasswordAuthentication(login, password);
            }
    
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return authentication;
            }
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        } 
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
    }
    

    make two other pages error.jsp and success.jsp to redirect to the particular page to see whether email sent or not…
    this code works for me
    I hope..now you can do it..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send an email from a Ruby program. The smtp server
I'm trying to send an email from my website that I've upgraded from .NET
I m trying to use smtp class from Python 2.6.4 to send smtp email
We are having problems when trying to send email from our java app. We
I'm trying to send an e-mail with Codeigniter like this: $this->load->library('email'); $this->email->from(myemail@email.com); $this->email->reply_to(myemail@email.com); $this->email->to(myemail@email.com);
I am trying to send email from Ant Script. here is the code snippet
I am trying to send an smtp email from my c# web site. it
I am trying to send an email from a site I am building, but
I am trying to send email using Exchange 2007 from a console app using
I am trying to send some text in an email from my cocoa app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.