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......