Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

send message in jsp form

hi,
I have jsp page that contain a form, What i want is :
when user click the submit button: an email notification will be send to (any email)
I work offline just to test the sending email, where i can find the sent email? is it in the c:\intpub\mailroot\Queue
this is my code, it didn't work,
i used netbeans with tomcat server , what is this lines means?
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
        String host = "smtp.yourisp.net";
what i should write insted of (yourisp) in case of working locally?
Note: the SMTP is installed in my computer

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
 
// Send a simple, single part, text/plain e-mail
public class TestEmail {
 
    public static void main(String[] args) {
 
        // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
        String to = "vipan@vipan.com";
        String from = "vipan@vipan.com";
        // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
        String host = "smtp.yourisp.net";
 
        // Create properties, get Session
        Properties props = new Properties();
 
        // If using static Transport.send(),
        // need to specify which host to send it to
        props.put("mail.smtp.host", host);
        // To see what is going on behind the scene
        props.put("mail.debug", "true");
        Session session = Session.getInstance(props);
 
        try {
            // Instantiatee a message
            Message msg = new MimeMessage(session);
 
            //Set message attributes
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("Test E-Mail through Java");
            msg.setSentDate(new Date());
 
            // Set message content
            msg.setText("This is a test of sending a " +
                        "plain text e-mail through Java.\n" +
                        "Here is line 2.");
 
            //Send the message
            Transport.send(msg);
        }
        catch (MessagingException mex) {
            // Prints all nested (chained) exceptions as well
            mex.printStackTrace();
        }
    }
}//End of class

Open in new window

0
master_programmer
Asked:
master_programmer
  • 5
  • 4
1 Solution
 
objectsCommented:
it want s the hostname of your smtp server
use localhost or 127.0.0.1 if your smtp server is running on the same machine
0
 
master_programmerAuthor Commented:
hi, it did not work
0
 
KuldeepchaturvediCommented:
what is the error messages that you are seeing, I see your code has the debug as true..
It shdould be telling exactly what is wrong with the mail if it is not successfully connecting to your smtp server.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
master_programmerAuthor Commented:
there is no error message, but i didn't recive any message? actually i did not know where exactly
the sent messages  will be ,if i am work locally ?
0
 
KuldeepchaturvediCommented:
the message shd be in Inetpub/mailroot/mailbox ... or queue folder where you wd find it.

(in windows env).
0
 
master_programmerAuthor Commented:
the messages did not send yet !!!! I use netbeans with tomcat apache server is this the problem ?
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
 
// Send a simple, single part, text/plain e-mail
public class TestEmail {
 
    public static void main(String[] args) {
 
        // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
        String to = "vipan@vipan.com";
        String from = "vipan@vipan.com";
        // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
        String host = "smtp.localhost.net";
 
        // Create properties, get Session
        Properties props = new Properties();
 
        // If using static Transport.send(),
        // need to specify which host to send it to
        props.put("mail.smtp.host", host);
        // To see what is going on behind the scene
        props.put("mail.debug", "true");
        Session session = Session.getInstance(props);
 
        try {
            // Instantiatee a message
            Message msg = new MimeMessage(session);
 
            //Set message attributes
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("Test E-Mail through Java");
            msg.setSentDate(new Date());
 
            // Set message content
            msg.setText("This is a test of sending a " +
                        "plain text e-mail through Java.\n" +
                        "Here is line 2.");
 
            //Send the message
            Transport.send(msg);
        }
        catch (MessagingException mex) {
            // Prints all nested (chained) exceptions as well
            mex.printStackTrace();
        }
    }
}//End of class

Open in new window

0
 
KuldeepchaturvediCommented:
if you are not finding the message, and system is not throwing any error.

then
1. You are not looking at the right places for the message
2. System is throwing a error which the code is not catching...
can you paste your debug log of code? it might give us some clues.
0
 
master_programmerAuthor Commented:
sorry but i don't know where is the debug log of code?
0
 
KuldeepchaturvediCommented:
It shd be either on your console. Or in stdout/stderr.logs file..

I thought in one of the post you said that there is no error message.. if you have not looked at the logs, how you can be certain that there are no error messages..
0
 
master_programmerAuthor Commented:
because when i run the page every thing is going ok ,but no message have been sent
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now