Connecting Tech Pros Worldwide Forums | Help | Site Map

Java Mail using JSP

ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,441
#1: Apr 17 '07
I am not getting any mail from this Script. What is wrong with the coding.Page is working perfectly with my SMTP provider and its giving the output in the page. but no m ail in my mail box.


Expand|Select|Wrap|Line Numbers
  1. <%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
  2. <%
  3. String from="ajaxrand@gmail.com";
  4. String to="ajaxrand@gmail.com";
  5. try
  6. {
  7. SmtpClient client = new SmtpClient("MY.SMTP.HERE");
  8. client.from(from);
  9. client.to(to);
  10. PrintStream message = client.startMessage();
  11. out.println("To: " + to+"<BR>");
  12. out.println("Subject: Sending email from JSP!<BR>");
  13. out.println("This was sent from a JSP page!<BR>");
  14. client.closeServer();
  15. }
  16. catch (IOException e)
  17. {
  18. out.println("ERROR SENDING EMAIL:"+e);
  19. }
  20. %>
  21.  

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Apr 17 '07

re: Java Mail using JSP


I don't understand what you're trying to do there. What is an SMTPClient?
This is how I send a mail message:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Properties;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4.  
  5. public class MailExample {
  6.   public static void main (String args[]) throws Exception {
  7.     String host = "smtp.xxxxx.xxx";
  8.     String to = "whatever@domain.topdomain";
  9.     String from = "tttttt@xxxxx.xxx";
  10.  
  11.     // Get system properties
  12.     Properties props = System.getProperties();
  13.  
  14.     // Setup mail server
  15.     props.put("mail.smtp.host", host);
  16.  
  17.     // Get session
  18.     Session session = Session.getDefaultInstance(props, null);
  19.  
  20.     session.setDebug(true);
  21.     // Define message
  22.     MimeMessage message = new MimeMessage(session);
  23.  
  24.     // Set the from address
  25.     message.setFrom(new InternetAddress(from));
  26.  
  27.     // Set the to address
  28.     message.addRecipient(Message.RecipientType.TO, 
  29.       new InternetAddress(to));
  30.  
  31.     // Set the subject
  32.     message.setSubject("Hello JavaMail");
  33.  
  34.     // Set the content
  35.     message.setText("Welcome to JavaMail");
  36.  
  37.     // Send message
  38.     Transport.send(message);
  39.   }
  40. }
kind regards,

Jos
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,441
#3: Apr 18 '07

re: Java Mail using JSP


Sorry this script is from this URL, I think i misunderstood the coding.
http://www.jguru.com/faq/view.jsp?EID=1163
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Apr 18 '07

re: Java Mail using JSP


Quote:

Originally Posted by ajaxrand

Sorry this script is from this URL, I think i misunderstood the coding.
http://www.jguru.com/faq/view.jsp?EID=1163

Don't just copy and paste code and hope for the best: that article didn't mention
the SmtpClient (at least not on the same page). You either have to read the
entire article or stick with the javax.mail.* classes. My little example uses just
those. Programmers who simply copy and paste stuff without understanding
*what* they've just copied and pasted are never, I repeat, never good programmers.

First you have to read and understand, then you experiment and only then you
try to come up with something useful; there is no other way, believe me.

kind regards,

Jos
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,441
#5: Apr 18 '07

re: Java Mail using JSP


I needed to use pure JSP mail Script. Not a class file like yours.
Then i have to go for servlets.
Any way i made it.

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="javax.mail.*, javax.mail.internet.*,java.util.*;" %>
  2. <%
  3. try
  4. {
  5. InternetAddress toAddress = new InternetAddress("To_mail_addres_here");
  6. InternetAddress fromAddress = new InternetAddress("From_mail_addres_here");
  7. Properties prp = new Properties();
  8. prp.put("mail.smtp.host", "some.smtp.here");
  9. Session mailsession = Session.getDefaultInstance(prp, null);
  10. Message msg = new MimeMessage(mailsession);
  11. msg.addRecipient(Message.RecipientType.TO, toAddress);
  12. msg.setSubject("HTML Mail");
  13. msg.setFrom(fromAddress);
  14. msg.setContent("<B>HTML Message</B>","text/html");
  15. Transport.send(msg);
  16. %>
  17. <b>An e-mail has been sent.</b>
  18. <%
  19. }
  20. catch (Exception e)
  21. e.printStackTrace();
  22. }
  23. %>
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Apr 18 '07

re: Java Mail using JSP


Quote:

Originally Posted by ajaxrand

I needed to use pure JSP mail Script. Not a class file like yours.
Then i have to go for servlets.
Any way i made it.

Good; notice that my class was just a MailExample class and everything was
done from the static main() method. Not much of a class and very easy to stick
in main's code elsewhere ;-)

Good you made it; best of luck.

kind regards,

Jos
Reply