Results 1 to 19 of 19
  1. #1
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default Send mail using a contactform

    Hi i cant get this to work at all with javamail, i need a servlet that sends a mail from a request from a simple html contact form with name,subject and message, tried it out with javamail but still cant get this to work. how can i fix this? help please!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    Without any code at all it's going to be very difficult to say where you're going wrong.

  3. #3
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    followed this tutorial but cant compile at step . how to send an email from jsp/servlet? - Stack Overflow

    and im new to java too so some help would be appreciated

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    Post your code here and the compilation error and point out which line it occurs on.

  5. #5
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
    public class TestMail {
        public static void main(String... args) throws Exception {
            // Create mailer.
            String hostname = "smtp.example.com";
            int port = 2525;
            String username = "nobody";
            String password = "idonttellyou";
            Mailer mailer = new Mailer(hostname, port, username, password);
    
            // Send mail.
            String from = "john.doe@example.com";
            String to = "jane.doe@example.com";
            String subject = "Interesting news";
            String message = "I've got JavaMail to work!";
            mailer.send(from, to, subject, message);
        }
    }

    TestMail.java:8: cannot find symbol
    symbol : class Mailer
    location: class TestMail
    Mailer mailer = new Mailer(hostname, port, username, password);
    ^
    TestMail.java:8: cannot find symbol
    symbol : class Mailer
    location: class TestMail
    Mailer mailer = new Mailer(hostname, port, username, password);
    ^
    2 errors

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    The compiler cannot find the Mailer class.
    Have you imported it into the TestMail class?

  7. #7
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Hmm no i havent, how do i make a Testmail class?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    Um, you wrote that code...which is called TestMail.
    It is creating a Mailer object, which is presumably a class described in that tutorial?
    You need to import it.

    If you don't know about importing classes then might I suggest starting with something simpler?

  9. #9
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Um, you wrote that code...which is called TestMail.
    It is creating a Mailer object, which is presumably a class described in that tutorial?
    You need to import it.

    If you don't know about importing classes then might I suggest starting with something simpler?
    yes but can you tell me what to do please? i really need this contactform.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    Do you know Java?
    Do you know what I mean by "import"?

  11. #11
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Do you know Java?
    Do you know what I mean by "import"?
    Well im a beginner and i made my first servlet justsome weeks ago, yes i know what you mean with import, i tried with this code

    Java Code:
       Properties props = new Properties();
        props.put("mail.smtp.host", "my-mail-server");
        props.put("mail.from", "me@example.com");
        Session session = Session.getInstance(props, null);
    
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom();
            msg.setRecipients(Message.RecipientType.TO,
                              "you@example.com");
            msg.setSubject("JavaMail hello world example");
            msg.setSentDate(new Date());
            msg.setText("Hello, world!\n");
            Transport.send(msg);
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);
        }

    cant get it to work with the html form that i want tough

    Java Code:
    <form action="contact" method="post">
        <p>Your email address: <input name="email">
        <p>Mail subject: <input name="subject">
        <p>Mail message: <textarea name="message"></textarea>
        <p><input type="submit">
    </form>

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    So this is now a completely different question.

    What do you mean by "can't get it to work"?

  13. #13
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    So this is now a completely different question.

    What do you mean by "can't get it to work"?
    well when i push the submit button nothing happens. can you help me with the right servlet code for that html form?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    Debug it?
    Do you get an exception on the server?

    These are all things you need to do...not me.

  15. #15
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Debug it?
    Do you get an exception on the server?

    These are all things you need to do...not me.
    why cant you just help me out with this?

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    Because I don't know what's going on on your system and until I do I cannot help.

    So, debug it (System.out.println() scattered around with menaingful messages would help), and ensure that any exceptions are properly logged...and you can find them.

  17. #17
    kroken is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Because I don't know what's going on on your system and until I do I cannot help.

    So, debug it (System.out.println() scattered around with menaingful messages would help), and ensure that any exceptions are properly logged...and you can find them.
    Ive got this now but i still cant send a message trough it.

    Java Code:
     import java.io.IOException;
     import java.util.Properties;
     import javax.servlet.*;
     import javax.servlet.http.*;
     import javax.mail.*;
     import javax.mail.internet.*;
     
     public class ContactServlet extends HttpServlet {
     
     	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
     		
     		String email = request.getParameter("email");
     		String subject = request.getParameter("subject");
     		String message = request.getParameter("message");
     		
     		if(!message.equals(null)){
     			Properties props = new Properties();
     			props.put("mail.smtp.host", "mail.mysmtp.com");
     			props.put("mail.smtp.port", "587");
     			
     	        	Session session = Session.getDefaultInstance(props, null);
     			
     		try {
     	                Message m = new MimeMessage(session);
     	                m.setFrom(new InternetAddress(email, "user"));
     	                m.addRecipient(Message.RecipientType.TO, new InternetAddress("mymail@myhost.com", "me"));
     	                m.setSubject(subject);
     	                m.setText(message);
     	                Transport.send(m);
     
     	        } catch (AddressException e) {
     	        	e.printStackTrace();
     	        } catch (javax.mail.MessagingException e) {
     		        e.printStackTrace();
     		}
     	        
     		RequestDispatcher view = request.getRequestDispatcher("sent.jsp");
     			
     		view.forward(request, response);
     
     		}
     	}
     
     }

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    And?
    Where does it get to?
    What are all those values?
    I see the printStackTrace()s, but no debug printing. Unless you;re stepping through it in a debugger in which case you'll be able to say what's going on better than I can guess.

  19. #19
    gb.rashu is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Hello,
    you are using 2 times props.put() method.
    put your smtp server ip address in instead of "mail.mysmtp.com"..
    try it.

Similar Threads

  1. Send and Receive mail using J2ME
    By chale in forum CLDC and MIDP
    Replies: 2
    Last Post: 08-07-2009, 11:44 AM
  2. Help, Please!!! Can't Send Mail with SmtpClient
    By jfcup in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-07-2008, 12:46 PM
  3. JSP send mail script not working.
    By profuse in forum Java Applets
    Replies: 1
    Last Post: 05-27-2008, 06:37 AM
  4. A Client to Send SMTP Mail
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:06 PM
  5. Send a pic through mail, in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •