Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

need help with jsp email

Posted on 2007-11-25
4
Medium Priority
?
2,990 Views
Last Modified: 2010-05-18
I have the following:

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.Session.*" %>
<%@ page import="javax.mail.internet.MimeMessage.*" %>
:
            try {
                Properties props = new Properties();
                props.put("mail.smtp.host", "127.0.0.1");
                Session s = Session.getInstance(props);
                MimeMessage message = new MimeMessage(s);
                InternetAddress from = new InternetAddress("mark@ohprs.org");
                message.setFrom(from);
                InternetAddress to = new InternetAddress(homeEmail);
                message.addRecipient(Message.RecipientType.TO, to);
                message.setSubject("OHPRS Pension Website");
                message.setText("Your new password is " + newPw);
                Transport.send(message);
            }
 
I get the errors:

Session cannot be resolved to a type
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
Session cannot be resolved
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
MimeMessage cannot be resolved to a type
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
MimeMessage cannot be resolved to a type
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
InternetAddress cannot be resolved to a type
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
InternetAddress cannot be resolved to a type
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
InternetAddress cannot be resolved to a type
InternetAddress cannot be resolved to a type
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
Message.RecipientType.TO cannot be resolved to a type
An error occurred at line: 204 in the jsp file: /forgotPassword.jsp
Transport cannot be resolved

Any idea what is wrong? I copied this code basically verbatim from another, successful, EE posting. I am using Linux, tomcat 6.0
0
Comment
Question by:jmarkfoley
[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
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 20348313
make sure the mail.jar and activation.jar are in the classpath or the lib folder of your webapplication!
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20349931
I do not appear to have mail.jar or activation.jar on this computer. Does that mean I have to install them?
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 1500 total points
ID: 20349942
download them, you can get it by Java Mail API
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20406876
ksivananth: I'm sure your solution is correct, so I will accept it as such. But, since email apparently doesn't acctually get shipped with Java and since I don't need it that much, I've decided to go with a system specific although encapsulated jsp function solution which I have include below.

Thanks -- I may get around to installing javamail one day when I'm bored!
<%@ page import="java.lang.Runtime, java.io.*, java.util.Random" %>
<%!
/*  -----------------------------------------------------------------
    The Java email package is not included with the standard release.
    Rather than install it, we will use the system interface instead.
    This will need to be changed if this application is ever moved
    off Linux/Unix. See emailJava.txt for code to use Java email.
    The email installation download is in javamail-1_4_1.zip
    somewhere on this disk.
    -----------------------------------------------------------------  */
 
private int runThis(String command)
{
    Process p = null;
 
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
    } catch ( Exception e) { return p.exitValue(); }
 
    return 0;
}
 
private int sendEmail(String to, String from, String subject, String message)
{
    FileOutputStream fs;
    PrintStream ps;
    Process p = null;
    String toAddr = null;
    String replyAddr = null;
    String adminAddr = null;
    String defaultReply = "mfoley@novatec-inc.com";
    int status = 0;
    Random r = new Random();
    String f = Long.toString(Math.abs(r.nextLong()), 36);
    String msgFile = "/tmp/" + f + ".sh";
 
    // Get web Administrator default address
 
    try {
        Context ctx = new javax.naming.InitialContext();
        Context env = (Context) ctx.lookup("java:comp/env");
        adminAddr = (String) env.lookup("webAdmin");
    }
    catch (NamingException e)
    {
        adminAddr = defaultReply;  // Probably webAdmin not bound
    }
    catch (Exception e)
    {
        return -2;
    }
 
    // If to address is null, use default (Web Admin)
 
    toAddr = ((to == null) || (to.length() == 0)) ? adminAddr : to;
 
    // If from address is null, get default configured address.
 
    replyAddr = ((from == null) || (from.length() == 0)) ? adminAddr : from;
 
    try {
        fs = new FileOutputStream(msgFile);
        ps = new PrintStream(fs);
    }
    catch (Exception e) { return -1; }
 
    ps.println("mail -s '" + subject + "' -r " + replyAddr + " " + toAddr + " << EOF");
    ps.println(message);
    ps.println("EOF");
    ps.close();
 
    status = runThis("chmod +rx " + msgFile);
    if (status != 0) return status;
 
    if ((status = runThis(msgFile)) != 0) return status;
 
    status = runThis("rm -f " + msgFile);
    if (status != 0) return status;
    return 0;
}
%>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 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