By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

send mail using java

P: 45
Hi to all I am a new member to this group. I have a problem in Email Sending using java. That is the mail is sending from my server to any desired mail account. By using the html i created the form but i am not able to send the mail .please any one give the solution to do my project effectively. I submit the html code for example form.

code:

<html>
<body>

<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</body>
</html>

Using this form i want to send the mail through server.Please send your suggestion to me.

Thankyou very much,
Regards
Geetha.
Jan 4 '07 #1
Share this Question
Share on Google+
12 Replies


10K+
P: 13,264
Hi to all I am a new member to this group. I have a problem in Email Sending using java. That is the mail is sending from my server to any desired mail account. By using the html i created the form but i am not able to send the mail .please any one give the solution to do my project effectively. I submit the html code for example form.

code:

<html>
<body>

<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</body>
</html>

Using this form i want to send the mail through server.Please send your suggestion to me.

Thankyou very much,
Regards
Geetha.
This may or may not have anything to do with java. To send mail using java one should have a look at the java mail api. I doubt that this is what you want to do.

Have a look at the mailto command and tell us if that's not what you want.
Jan 4 '07 #2

P: 45
This may or may not have anything to do with java. To send mail using java one should have a look at the java mail api. I doubt that this is what you want to do.

Have a look at the mailto command and tell us if that's not what you want.
Thank you for your reply. the html code i send is only for reference for that format i want to send a mail using java servlets or also using the jsp .that is the email is send to our customers through my server (Tomcat web server).
Jan 4 '07 #3

10K+
P: 13,264
Thank you for your reply. the html code i send is only for reference for that format i want to send a mail using java servlets or also using the jsp .that is the email is send to our customers through my server (Tomcat web server).
Ah, well then if you are using j2ee then
http://www.javapractices.com/Topic144.cjp is probably what you need.
Jan 4 '07 #4

100+
P: 157
Hi to all I am a new member to this group. I have a problem in Email Sending using java. That is the mail is sending from my server to any desired mail account. By using the html i created the form but i am not able to send the mail .please any one give the solution to do my project effectively. I submit the html code for example form.

code:

<html>
<body>

<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</body>
</html>

Using this form i want to send the mail through server.Please send your suggestion to me.

Thankyou very much,
Regards
Geetha.
Hi to all I am a new member to this group. I have a problem in Email Sending using java. That is the mail is sending from my server to any desired mail account. By using the html i created the form but i am not able to send the mail .please any one give the solution to do my project effectively. I submit the html code for example form.

code:

<html>
<body>

<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</body>
</html>

Using this form i want to send the mail through server.Please send your suggestion to me.

Thankyou very much,
Regards
Geetha.
-------------------------------------------------------------------------------------------------------------------
Hi,
If u are using a Servlet for processing then u need to add the <action> tag in ur Html page above the <body> tag..This is how ur Html page should look like :-

<html>

<form method="POST" action="http://localhost:8000/Geethu/SM">

<body>

<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</body>
</html>

Now depending on wt Http-method(Get,Post,Head etc.. ) u want to use..the most common being "Get" and "Post"..u mention it in the method-attribute of <form> ..
Here we are using Post and note tht even in the corresponding HttpServlet class u hv to use "doPost()" ..

In the action attribute of the <form> I have given the URL for the HttpServlet which needs to be invoked after hitting the 'Submit' button,
If u r working with J2EE then u must be familiar with the terms URL and that it comprises of : 'protocol','machine name and number','port number','resources' ,

I have deployed this prg in J2ee server where we have a 'context-root' and 'alias' during deployment to identify the web-application & resource respectively
(in this particular program the resource happens to be the servlet :- SendMailServlet identified by alias -"SM" and I've assumed the context root to be "Geethu") ,

Now wt we are doing is we are collecting the info frm the Html Page and passing it to our Servlet which we hv named: SendMailServlet.java and this Servlet in turn will handle the EMail-send operation.

The Servlet program:-

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;

public class SendMailServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String body = request.getParameter("body");

sendEmail(to,from,subject,body) ; //call to the sendEmail() method
}

protected void sendEmail(String a,String b,String c,String d)
{
String toSomeone = a;
String fromMe = b;
String subj = c;
String content = d;

try
{
//Set up the default parameters

Properties props = new Properties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host","--*N-O-T-E*----"); // *Note*:- here u need to write the name of ur smtp_server
props.put("mail.smtp.port","25");

//Create the session and create a new mail message
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);

//Set the From,To,Date and Subject fields
msg.setFrom(new InternetAddress(fromMe)); //from
msg.setRecipients(Message.RecipientType.TO,Interne tAddress.parse(toSomeone)); //to
msg.setSentDate(new java.util.Date());
msg.setSubject(subj); //subject of ur mail
msg.setText(content); //body of ur mail

Transport.send(msg);

msg.writeTo(System.out);

}
catch(Exception e)
{
System.out.println(e);
}

}//sendEmail() over

}//SendMailServlet over

First specify the name of ur smtp_Server in the above servlet at the marked posn, compile the sevlet prg, add it to ur web-application, deploy, and chek the o/p

To get a better idea of the classes and methods etc used here u cn refer to the chapter "JavaMail" from the book "Professional Java Server Programming"

Hope this helps......
Jan 6 '07 #5

P: 45
-------------------------------------------------------------------------------------------------------------------
Hi,
If u are using a Servlet for processing then u need to add the <action> tag in ur Html page above the <body> tag..This is how ur Html page should look like :-

<html>

<form method="POST" action="http://localhost:8000/Geethu/SM">

<body>

<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</body>
</html>

Now depending on wt Http-method(Get,Post,Head etc.. ) u want to use..the most common being "Get" and "Post"..u mention it in the method-attribute of <form> ..
Here we are using Post and note tht even in the corresponding HttpServlet class u hv to use "doPost()" ..

In the action attribute of the <form> I have given the URL for the HttpServlet which needs to be invoked after hitting the 'Submit' button,
If u r working with J2EE then u must be familiar with the terms URL and that it comprises of : 'protocol','machine name and number','port number','resources' ,

I have deployed this prg in J2ee server where we have a 'context-root' and 'alias' during deployment to identify the web-application & resource respectively
(in this particular program the resource happens to be the servlet :- SendMailServlet identified by alias -"SM" and I've assumed the context root to be "Geethu") ,

Now wt we are doing is we are collecting the info frm the Html Page and passing it to our Servlet which we hv named: SendMailServlet.java and this Servlet in turn will handle the EMail-send operation.

The Servlet program:-

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;

public class SendMailServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String body = request.getParameter("body");

sendEmail(to,from,subject,body) ; //call to the sendEmail() method
}

protected void sendEmail(String a,String b,String c,String d)
{
String toSomeone = a;
String fromMe = b;
String subj = c;
String content = d;

try
{
//Set up the default parameters

Properties props = new Properties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host","--*N-O-T-E*----"); // *Note*:- here u need to write the name of ur smtp_server
props.put("mail.smtp.port","25");

//Create the session and create a new mail message
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);

//Set the From,To,Date and Subject fields
msg.setFrom(new InternetAddress(fromMe)); //from
msg.setRecipients(Message.RecipientType.TO,Interne tAddress.parse(toSomeone)); //to
msg.setSentDate(new java.util.Date());
msg.setSubject(subj); //subject of ur mail
msg.setText(content); //body of ur mail

Transport.send(msg);

msg.writeTo(System.out);

}
catch(Exception e)
{
System.out.println(e);
}

}//sendEmail() over

}//SendMailServlet over

First specify the name of ur smtp_Server in the above servlet at the marked posn, compile the sevlet prg, add it to ur web-application, deploy, and chek the o/p

To get a better idea of the classes and methods etc used here u cn refer to the chapter "JavaMail" from the book "Professional Java Server Programming"

Hope this helps......

Hi
Thank you for your code. i done as you tell but at the time of complie i have some error pls tell what is the problem. any jar files are added for this program.
I already have the mail and activation jar files in my lib directory. I am in confusion please help me.

The Errors are
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapp
ses>javac SendMailServlet.java
SendMailServlet.java:5: package javax.mail does not exist
import javax.mail.*;
^
SendMailServlet.java:6: package javax.activation does not exi
import javax.activation.*;
^
SendMailServlet.java:7: package javax.mail.internet does not
import javax.mail.internet.*;
^
SendMailServlet.java:39: cannot find symbol
symbol : class Session
location: class SendMailServlet
Session session = Session.getInstance(props);
^
SendMailServlet.java:39: cannot find symbol
symbol : variable Session
location: class SendMailServlet
Session session = Session.getInstance(props);
^
SendMailServlet.java:40: cannot find symbol
symbol : class Message
location: class SendMailServlet
Message msg = new MimeMessage(session);
^
SendMailServlet.java:40: cannot find symbol
symbol : class MimeMessage
location: class SendMailServlet
Message msg = new MimeMessage(session);
^
SendMailServlet.java:43: cannot find symbol
symbol : class InternetAddress
location: class SendMailServlet
msg.setFrom(new InternetAddress(fromMe)); //from

Thanks
Jan 9 '07 #6

100+
P: 157
Hi
Thank you for your code. i done as you tell but at the time of complie i have some error pls tell what is the problem. any jar files are added for this program.
I already have the mail and activation jar files in my lib directory. I am in confusion please help me.

The Errors are
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapp
ses>javac SendMailServlet.java
SendMailServlet.java:5: package javax.mail does not exist
import javax.mail.*;
^
SendMailServlet.java:6: package javax.activation does not exi
import javax.activation.*;
^
SendMailServlet.java:7: package javax.mail.internet does not
import javax.mail.internet.*;
^
SendMailServlet.java:39: cannot find symbol
symbol : class Session
location: class SendMailServlet
Session session = Session.getInstance(props);
^
SendMailServlet.java:39: cannot find symbol
symbol : variable Session
location: class SendMailServlet
Session session = Session.getInstance(props);
^
SendMailServlet.java:40: cannot find symbol
symbol : class Message
location: class SendMailServlet
Message msg = new MimeMessage(session);
^
SendMailServlet.java:40: cannot find symbol
symbol : class MimeMessage
location: class SendMailServlet
Message msg = new MimeMessage(session);
^
SendMailServlet.java:43: cannot find symbol
symbol : class InternetAddress
location: class SendMailServlet
msg.setFrom(new InternetAddress(fromMe)); //from

Thanks
Hi,

Have you set the classpath? i.e the path of the jar file of the server that you are using?
For eg:-
I am using J2EE server so my classpath is like this:-
.;C:\j2sdkee1.3.1\lib\j2ee.jar

If you were using Weblogic then it would be :-
.;C:\j2sdkee1.3.1\lib\weblogic.jar


so accordingly whichever server you are using do keep the jar file for the same in the classpath , the jar file contains the entire API so without keeping it in the classpath you wont be able to compile successfully, please check ..
Jan 9 '07 #7

100+
P: 157
Hi again,

I think by mistake I might have specified the classpath for Weblogic incorrectly, but anyways I think you are using Tomcat so you might find the foll link useful

http://www.coreservlets.com/Apache-Tomcat-Tutorial/#Set-CLASSPATH

Please do check ...
Jan 9 '07 #8

P: 1
Hi to all I am a new member to this group. I have a problem in Email Sending using java. That is the mail is sending from my server to any desired mail account. By using the html i created the form but i am not able to send the mail .please any one give the solution to do my project effectively. I submit the html code for example form.

code:

<html>
<body>

<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</body>
</html>

Using this form i want to send the mail through server.Please send your suggestion to me.

Thankyou very much,
Regards
Geetha.


Hi iam new to java mail can u pls send me the complete codings of sending mail.......My id <Removed: against site rules>
Mar 6 '07 #9

P: 45
Hi iam new to java mail can u pls send me the complete codings of sending mail.......My id [deleted]
Hi Anand
Sendingmail using JAVA is very complex. So done it by using the PHP.Its very easy to compare the Java. If you want the Php code i will send it to you. Php code is same like html code.

Reply me if you want the code,

Regards,
Geethu.
Mar 6 '07 #10

10K+
P: 13,264
Hi Anand
Sendingmail using JAVA is very complex. So done it by using the PHP.Its very easy to compare the Java. If you want the Php code i will send it to you. Php code is same like html code.

Reply me if you want the code,

Regards,
Geethu.
No it's not complex. The java mail API is very easy to use if you know a bit of Java. I think the PHP is easier for PHP developers.
Mar 6 '07 #11

P: 45
No it's not complex. The java mail API is very easy to use if you know a bit of Java. I think the PHP is easier for PHP developers.
yes I know little bit of Java. But i don't know about mail API. Please give the details about the mail API.

Thank you,
Geethu
Mar 6 '07 #12

P: 3
Hai guys can any one of u guide me how to send mail to a mailing list using java
Feb 8 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.