send mail using java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geethu03
    New Member
    • Dec 2006
    • 45

    #1

    send mail using java

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Geethu03
    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.

    Comment

    • Geethu03
      New Member
      • Dec 2006
      • 45

      #3
      Originally posted by r035198x
      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).

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Geethu03
        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.

        Comment

        • abctech
          New Member
          • Dec 2006
          • 157

          #5
          Originally posted by Geethu03
          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.
          Originally posted by Geethu03
          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','mac hine name and number','port number','resour ces' ,

          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.h ttp.*;
          import java.io.*;

          import javax.mail.*;
          import javax.activatio n.*;
          import javax.mail.inte rnet.*;
          import java.util.*;

          public class SendMailServlet extends HttpServlet
          {
          protected void doPost(HttpServ letRequest request,HttpSer vletResponse response) throws ServletExceptio n,IOException
          {
          String to = request.getPara meter("to");
          String from = request.getPara meter("from");
          String subject = request.getPara meter("subject" );
          String body = request.getPara meter("body");

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

          protected void sendEmail(Strin g 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.prot ocol","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.getInst ance(props);
          Message msg = new MimeMessage(ses sion);

          //Set the From,To,Date and Subject fields
          msg.setFrom(new InternetAddress (fromMe)); //from
          msg.setRecipien ts(Message.Reci pientType.TO,In ternetAddress.p arse(toSomeone) ); //to
          msg.setSentDate (new java.util.Date( ));
          msg.setSubject( subj); //subject of ur mail
          msg.setText(con tent); //body of ur mail

          Transport.send( msg);

          msg.writeTo(Sys tem.out);

          }
          catch(Exception e)
          {
          System.out.prin tln(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 "Profession al Java Server Programming"

          Hope this helps......

          Comment

          • Geethu03
            New Member
            • Dec 2006
            • 45

            #6
            Originally posted by abctech
            -------------------------------------------------------------------------------------------------------------------
            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','mac hine name and number','port number','resour ces' ,

            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.h ttp.*;
            import java.io.*;

            import javax.mail.*;
            import javax.activatio n.*;
            import javax.mail.inte rnet.*;
            import java.util.*;

            public class SendMailServlet extends HttpServlet
            {
            protected void doPost(HttpServ letRequest request,HttpSer vletResponse response) throws ServletExceptio n,IOException
            {
            String to = request.getPara meter("to");
            String from = request.getPara meter("from");
            String subject = request.getPara meter("subject" );
            String body = request.getPara meter("body");

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

            protected void sendEmail(Strin g 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.prot ocol","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.getInst ance(props);
            Message msg = new MimeMessage(ses sion);

            //Set the From,To,Date and Subject fields
            msg.setFrom(new InternetAddress (fromMe)); //from
            msg.setRecipien ts(Message.Reci pientType.TO,In ternetAddress.p arse(toSomeone) ); //to
            msg.setSentDate (new java.util.Date( ));
            msg.setSubject( subj); //subject of ur mail
            msg.setText(con tent); //body of ur mail

            Transport.send( msg);

            msg.writeTo(Sys tem.out);

            }
            catch(Exception e)
            {
            System.out.prin tln(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 "Profession al 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\Tomc at 5.5\webapp
            ses>javac SendMailServlet .java
            SendMailServlet .java:5: package javax.mail does not exist
            import javax.mail.*;
            ^
            SendMailServlet .java:6: package javax.activatio n does not exi
            import javax.activatio n.*;
            ^
            SendMailServlet .java:7: package javax.mail.inte rnet does not
            import javax.mail.inte rnet.*;
            ^
            SendMailServlet .java:39: cannot find symbol
            symbol : class Session
            location: class SendMailServlet
            Session session = Session.getInst ance(props);
            ^
            SendMailServlet .java:39: cannot find symbol
            symbol : variable Session
            location: class SendMailServlet
            Session session = Session.getInst ance(props);
            ^
            SendMailServlet .java:40: cannot find symbol
            symbol : class Message
            location: class SendMailServlet
            Message msg = new MimeMessage(ses sion);
            ^
            SendMailServlet .java:40: cannot find symbol
            symbol : class MimeMessage
            location: class SendMailServlet
            Message msg = new MimeMessage(ses sion);
            ^
            SendMailServlet .java:43: cannot find symbol
            symbol : class InternetAddress
            location: class SendMailServlet
            msg.setFrom(new InternetAddress (fromMe)); //from

            Thanks

            Comment

            • abctech
              New Member
              • Dec 2006
              • 157

              #7
              Originally posted by Geethu03
              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\Tomc at 5.5\webapp
              ses>javac SendMailServlet .java
              SendMailServlet .java:5: package javax.mail does not exist
              import javax.mail.*;
              ^
              SendMailServlet .java:6: package javax.activatio n does not exi
              import javax.activatio n.*;
              ^
              SendMailServlet .java:7: package javax.mail.inte rnet does not
              import javax.mail.inte rnet.*;
              ^
              SendMailServlet .java:39: cannot find symbol
              symbol : class Session
              location: class SendMailServlet
              Session session = Session.getInst ance(props);
              ^
              SendMailServlet .java:39: cannot find symbol
              symbol : variable Session
              location: class SendMailServlet
              Session session = Session.getInst ance(props);
              ^
              SendMailServlet .java:40: cannot find symbol
              symbol : class Message
              location: class SendMailServlet
              Message msg = new MimeMessage(ses sion);
              ^
              SendMailServlet .java:40: cannot find symbol
              symbol : class MimeMessage
              location: class SendMailServlet
              Message msg = new MimeMessage(ses sion);
              ^
              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 ..

              Comment

              • abctech
                New Member
                • Dec 2006
                • 157

                #8
                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.coreservlet s.com/Apache-Tomcat-Tutorial/#Set-CLASSPATH

                Please do check ...

                Comment

                • anand83k
                  New Member
                  • Mar 2007
                  • 1

                  #9
                  Originally posted by Geethu03
                  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>

                  Comment

                  • Geethu03
                    New Member
                    • Dec 2006
                    • 45

                    #10
                    Originally posted by anand83k
                    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.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by Geethu03
                      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.

                      Comment

                      • Geethu03
                        New Member
                        • Dec 2006
                        • 45

                        #12
                        Originally posted by r035198x
                        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

                        Comment

                        • KriSh42
                          New Member
                          • Feb 2008
                          • 3

                          #13
                          Hai guys can any one of u guide me how to send mail to a mailing list using java

                          Comment

                          Working...