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:jshao745@gmail.com"
                        + "&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.

user3014926
  • 1,061
  • 7
  • 18
  • 26
  • 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. – Marc B Jan 09 '14 at 19:41
  • OK, how do I achieve them? – user3014926 Jan 09 '14 at 19:44

1 Answers1

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

Community
  • 1
  • 1
Diversity
  • 1,890
  • 13
  • 20
  • You mean I can follow these tutorials? (http://stackoverflow.com/questions/3757442/how-to-send-an-email-from-jsp-servlet) – user3014926 Jan 09 '14 at 21:26
  • Thank you for the update. Which jar do I need to download into my project? (https://java.net/projects/javamail/pages/Home) I am using java 7 though. – user3014926 Jan 10 '14 at 01:52
  • But it depends really of your web/application server. Often they are shipped with the appropriate API – Diversity Jan 10 '14 at 01:55