2

I am making a email submit form in html and the email is sent with Microsoft Outlook when user hit "submit". Here is my code:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
    function sendMail() {
        var link = "mailto:[email protected]"
                        + "&subject=" + encodeURIComponent (document.getElementById('subject').value)
                        + "&body=" + encodeURIComponent (document.getElementById('body').value)
                ;

        window.location.href = link;
    }
</script>
<style>
    textarea
    {
        resize: none;
    }
</style>
</head>
<body>
subject:<br>
<input type="text" name="subject" id="subject"><br>
Type your message here:<br>
<textarea rows="4" cols="50" id="body">
    This is a email testing.
</textarea>
<button onclick="sendMail(); return false">Send</button>
</body>
</html>

How do I make change so that the email is directory sent from webpage (or JSP to be exact) to my gmail without going through Outlook? I also try to add input field for sender's email address and name in the above form. I know I need to have a servlet for this to work.

I use java 7 and tomcat 7.0.

I appreciate if someone could help me.

2
  • 1
    it's not "going through outlook". it's going through the user's default mail client, which in your case happens to be outlook. You need server-side code so you DON'T invole the user's mail client. Commented Jan 9, 2014 at 19:41
  • OK, how do I achieve them? Commented Jan 9, 2014 at 19:44

1 Answer 1

3

Create a servlet and submit the forms contents to the servlet. The servlet itself can use the JavaMail API see: http://www.oracle.com/technetwork/java/javamail/index.html to send the email to the given receiver.

After the servlet has sent the mail you can redirect or forward to any jsp page.

UPDATE, due to comment:

Yes right. how to send an email from jsp/servlet?

But use the 8 voted solution. Don't insert your EMail api call within your JSP page. Send your forms content as simple http request to an approriate Servlet. Within that Servlet you are able to due anything that the Tomcat server allows.

Some hint:

The JavaScipt will be executed on client side and uses just preconfigured EMail-Client. When you use the EMail API everthing happens on server side. This means you pretend to be an EMail Server using the SMTP Protocol. Have a look here: http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

Sign up to request clarification or add additional context in comments.

3 Comments

You mean I can follow these tutorials? (stackoverflow.com/questions/3757442/…)
Thank you for the update. Which jar do I need to download into my project? (java.net/projects/javamail/pages/Home) I am using java 7 though.
But it depends really of your web/application server. Often they are shipped with the appropriate API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.