Solved

How do I send email from JSP?

Posted on 2004-09-08
5
403 Views
Last Modified: 2010-04-01
I have JSP running on Unix, and I want to send emails to customers.  
Can someone help with sample code?
0
Comment
Question by:g118481
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12006500
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12006506
If you are familiar with taglib, this is an easier option for you

http://jakarta.apache.org/taglibs/doc/mailer-doc/intro.html

Hope this help
0
 

Expert Comment

by:kheefatt
ID: 12006956
Sorry in a hurry. I will paste to u some sample codes tomorrow morning..8 hrs from now. Check back!
0
 

Accepted Solution

by:
kheefatt earned 100 total points
ID: 12012647
mail_form.html
------------------

<html>
<head>
</head>
<%@ page language="java" import="java.sql.*, java.util.ArrayList" %>
<body>
<form name="SubForm" method="post" action="send_mail.jsp">
      <table width="50%" border="1" >
      <tr>
            <td width="108" height="34">To : </td>
            <td width="363">
            <input type="text" name="receiver">
            </td>
      </tr>
      <tr>
            <td height="80" >Message : </td>
            <td><textarea name="mailtext"></textarea>
            </td>
      </tr>
      <tr>
            <td height="34" valign="top">&nbsp;</td>
            <td valign="top"><input type="submit" name="submit" value="Submit Information">
            </td>
      </tr>
      </table>
</form>
</body>
</html>

send_mail.jsp
----------------

<%@ page language="java" import="java.sql.*, java.util.ArrayList" %>
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<html>
<head>
</head>
<body>
<table width="50%" border="0" >
<tr>
<td>
<%
      String emailAdd = request.getParameter("receiver");
      String mailBody = request.getParameter("mailtext");
      boolean mail_error = false;
      String errMsg = "";

      try
      {
            Properties props = new Properties();
            props.put("mail.smtp.host", "127.0.0.1");  //replace 127.0.0.1 with the name u specify in your mail server
            Session s = Session.getInstance(props,null);
            MimeMessage message = new MimeMessage(s);
            InternetAddress from = new InternetAddress(emailAdd);
            message.setFrom(from);
            InternetAddress to = new InternetAddress("kheefatt@yahoo.com"); //your email address
            message.addRecipient(Message.RecipientType.TO, to);
            message.setSubject("Mail Test!");
            message.setText(mailBody);
            Transport.send(message);
      }
      catch (Exception ex)
      {
            String titleMsg = "Error Exception";
            errMsg = ex.getMessage();
            mail_error = true;
      }      
      if (mail_error==false)
            out.write("Thank you for subscribing to our newsletter.<BR>");
      else if (mail_error==true)
      {
            out.write("There's an error in the mail server. Please try again later!<BR>\n");
      }
%>
</td>
</tr>
</table>
</body>
</html>

Remember that you need a mail server. You can one free here. The mail server must be running when executing this mail function! All the best to you!

http://www.softrom.net/download_12082.html
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12016304
good sample kheefatt. :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading data  from excel sheet and displaying 1 89
iterator example remove 8 75
TreeSet comparator example 7 97
Why my table column Id is not passed to java object? 4 44
Most MSPs worth their salt are already offering cybersecurity to their customers. But cybersecurity as a service is wide encompassing and can mean many things.  So where are MSPs falling in this spectrum?
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question