Solved

How do I send email from JSP?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web service Rest Client creation 4 86
Jquery - parsing the results returned from webmethod 1 112
Coldfusion 10 - JSP File Not Found - 404 Error 5 92
login form jsp example 2 25
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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