Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JSP (http://www.go4expert.com/forums/jsp/)
-   -   JavaMAil in JSP (http://www.go4expert.com/forums/javamail-in-jsp-t1673/)

casimirrex 19Oct2006 10:00

JavaMAil in JSP
 
Hello Good morning to all of you,<p>
I'm newbie in JSP programming area,I've doubt on <p>JSP using JavaMail package.<p><p>
I can send and recive to my mail with in the my <p>server only,even as i can recive the my mail server <p>from any server.but i couldn't send to other mail <p>server like yahoo,hotmail and rediffmail etc from <p>my server.<p>
In this mail you can see my code as well.<p>
Code: JSP

/*mailF.htm
 --------------*/
 <html>
 <body>
   <form action="sendMail.jsp" method="post">
     <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>
   </form>
 </body>
 </html>

-----------------------------------------------------------
sendMail.JSP
-------------------
Code: JSP

<html>
   <head>
     <title>JSP JavaMail Example </title>
   </head>
 
 <body>
 
 <%@ page import="java.util.*" %>
 <%@ page import="javax.mail.*" %>
 <%@ page import="javax.mail.internet.*" %>
 <%@ page import="javax.activation.*" %>
 
 <%
     String host = "mail.rex.com";
     String to = request.getParameter("to");
     String from = request.getParameter("from");
     String subject = request.getParameter("subject");
     String messageText = request.getParameter("body");
     boolean sessionDebug = false;
 
     Properties props = System.getProperties();
     props.put("mail.host", host);
     props.put("mail.transport.protocol", "smtp");
 
     Session mailSession = Session.getDefaultInstance(props, null);
 
     mailSession.setDebug(sessionDebug);
 
     Message msg = new MimeMessage(mailSession);
 
     msg.setFrom(new InternetAddress(from));
     InternetAddress[] address = {new <p>InternetAddress(to)};
     msg.setRecipients(Message.RecipientType.TO, <p>address);
     msg.setSubject(subject);
     msg.setSentDate(new Date());
     msg.setText(messageText);

     Transport.send(msg);
     
     out.println("Mail was sent to " + to);
     out.println(" from " + from);
     out.println(" using host " + host + ".");
 
 %>
     </table>
   </body>
 </html>

Any one can bear my hand.
Thanks in advance
regards
rex

pradeep 20Oct2006 12:31

Re: JavaMAil in JSP
 
Many mails servers have strict rules w.r.t accepting mails. You have to specify the 'host' and the 'host' should be a valid mail server.
Mail servers do a resverse lookup on the IP of sever which has connected to it.


All times are GMT +5.5. The time now is 13:16.