?
Solved

How do I send email from JSP?

Posted on 2004-09-08
5
Medium Priority
?
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

770 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