Solved

How do I send email from JSP?

Posted on 2004-09-08
5
393 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now