Results 1 to 19 of 19
Thread: Send mail using a contactform
- 11-11-2010, 03:01 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
- 11-11-2010, 08:55 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Without any code at all it's going to be very difficult to say where you're going wrong.
- 11-11-2010, 03:49 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
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
- 11-11-2010, 04:32 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Post your code here and the compilation error and point out which line it occurs on.
- 11-11-2010, 05:34 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
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
- 11-11-2010, 05:36 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The compiler cannot find the Mailer class.
Have you imported it into the TestMail class?
- 11-11-2010, 05:38 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Hmm no i havent, how do i make a Testmail class?
- 11-11-2010, 05:39 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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?
- 11-11-2010, 05:44 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
- 11-12-2010, 08:26 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Do you know Java?
Do you know what I mean by "import"?
- 11-12-2010, 12:41 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
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>
- 11-12-2010, 12:57 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
So this is now a completely different question.
What do you mean by "can't get it to work"?
- 11-12-2010, 01:49 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
- 11-12-2010, 01:58 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Debug it?
Do you get an exception on the server?
These are all things you need to do...not me.
- 11-12-2010, 02:04 PM #15
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
- 11-12-2010, 02:10 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-14-2010, 08:04 PM #17
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
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); } } }
- 11-15-2010, 09:54 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-19-2010, 07:31 AM #19
Member
- Join Date
- Aug 2010
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Send and Receive mail using J2ME
By chale in forum CLDC and MIDPReplies: 2Last Post: 08-07-2009, 11:44 AM -
Help, Please!!! Can't Send Mail with SmtpClient
By jfcup in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11-07-2008, 12:46 PM -
JSP send mail script not working.
By profuse in forum Java AppletsReplies: 1Last Post: 05-27-2008, 06:37 AM -
A Client to Send SMTP Mail
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:06 PM -
Send a pic through mail, in java
By lenny in forum Advanced JavaReplies: 1Last Post: 07-25-2007, 02:49 PM


LinkBack URL
About LinkBacks


Bookmarks