1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

JavaMAil in JSP

Discussion in 'JSP' started by casimirrex, Oct 19, 2006.

  1. casimirrex

    casimirrex New Member

    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:
    /*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:
     <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
     
    Last edited by a moderator: Oct 19, 2006
  2. pradeep

    pradeep Team Leader

    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.
     

Share This Page