473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Java Mail using JSP

ak1dnar
1,584 Expert 1GB
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.  
Apr 17 '07 #1
5 3402
JosAH
11,448 Expert 8TB
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
Apr 17 '07 #2
ak1dnar
1,584 Expert 1GB
Sorry this script is from this URL, I think i misunderstood the coding.
http://www.jguru.com/faq/view.jsp?EID=1163
Apr 18 '07 #3
JosAH
11,448 Expert 8TB
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
Apr 18 '07 #4
ak1dnar
1,584 Expert 1GB
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. %>
Apr 18 '07 #5
JosAH
11,448 Expert 8TB
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
Apr 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
0
by: aychai | last post by:
Hi all, I am trying to make use Oracle9i db to call an external .Net Web Service from a JAva Stored Procedure. I created the .Net Web Service and use JDeveloper 9.0.3.4Build(1247) to create...
8
by: Eagle35 | last post by:
hi all im new to the whole java thing and need some help! im trying to make an e mail form and when i test it, it sends the e mail to the designated e mail address but when i go to the e mail...
2
by: JohnK | last post by:
Hi all, This is my first time visiting this group. I already tried two other java groups, but those aren't very populated. I don't know if it is the right one, otherwise please tell me in which...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
3
by: crossroadsk | last post by:
<pre> Hi all, I'm new to Javamail concept and i got the following exceptions when i tried to send a simple mail. First exception is : using java Could not connect to SMTP host:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.