Solved

send message in jsp form

Posted on 2007-12-01
10
625 Views
Last Modified: 2013-12-17
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
Comment
Question by:master_programmer
  • 5
  • 4
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20389391
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
 

Author Comment

by:master_programmer
ID: 20390789
hi, it did not work
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20415326
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:master_programmer
ID: 20418765
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20419826
the message shd be in Inetpub/mailroot/mailbox ... or queue folder where you wd find it.

(in windows env).
0
 

Author Comment

by:master_programmer
ID: 20437680
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
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 20439051
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
 

Author Comment

by:master_programmer
ID: 20440492
sorry but i don't know where is the debug log of code?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20443298
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
 

Author Comment

by:master_programmer
ID: 20445624
because when i run the page every thing is going ok ,but no message have been sent
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
Easy CSR creation in Exchange 2007,2010 and 2013
In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…

773 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