Sending mail from JSP

I'm trying to send an email from jsp from my work computer.

I am using the sun.net.smtp.SmtpClient as follows:

<%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
<%
  String from="kmcghee@campus.ie";
  String to="kmcghee@campus.ie";
  try{
      SmtpClient client = new SmtpClient(); // assumes localhost
      client.from(from);
      client.to(to);
      PrintStream message = client.startMessage();
      message.println("To: " + to);
      message.println();
      message.println("Email sent from jsp! :-)");
      message.println();
      message.println();
      client.closeServer();
   }
   catch (IOException e){      
      System.out.println("ERROR SENDING EMAIL:"+e);
   }
%>

I couldn't get my company's email server to send the email (Perhaps because its an exchange server) so I have installed a smtp server on my machine.

The server is trying to send the mail but I think my company's network is blocking the sending of the mail. I am getting this:

25/03/2006 14:57:40   1088 Connect from 127.0.0.1
25/03/2006 14:57:40   1088 New mail received. From:< kmcghee@campus.ie > to:< kmcghee@campus.ie >
25/03/2006 14:57:40   Message from:< kmcghee@campus.ie > to:< kmcghee@campus.ie >   Direct delivery fail! 5.7.1 IRDU-FPI-MAIN-2.Net.MYCOMPANYSDOMAIN.Com Connection not authorized Wait for retry!
25/03/2006 14:57:40   1088 Close connection.
25/03/2006 14:58:10   Message from:< kmcghee@campus.ie > to:< kmcghee@campus.ie >   Direct delivery fail! 5.7.1 IRDU-FPI-MAIN-1.NET.MYCOMPANYSDOMAIN.COM Connection not authorized Wait for retry!
25/03/2006 14:58:41   Message from:< kmcghee@campus.ie > to:< kmcghee@campus.ie >   Direct delivery fail! 5.7.1 IRDU-FPI-MAIN-1.NET.MYCOMPANYSDOMAIN.COM Connection not authorized Wait for retry!
25/03/2006 14:59:11   Message from:< kmcghee@campus.ie > to:< kmcghee@campus.ie >   Direct delivery fail! 5.7.1 IRDU-FPI-MAIN-1.NET.MYCOMPANYSDOMAIN.COM Connection not authorized Wait for retry!
25/03/2006 14:59:41   Message from:< kmcghee@campus.ie > to:< kmcghee@campus.ie >   Direct delivery fail! 5.7.1 IRDU-FPI-MAIN-1.NET.MYCOMPANYSDOMAIN.COM Connection not authorized
25/03/2006 15:00:12   Failed to send failure notify message to <kmcghee@campus.ie>!

Does anyone have any ways around this? Do you know what the problem is exactly? Is there a better way I can send a simple email from jsp like this?

Best Regards,

Kev
LVL 1
kmcgheeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
http://www.java2s.com/Code/Java/JSP/SendEmailFromJsp.htm
http://www.java2s.com/Code/Java/Servlets/Email.htm

any of the above links may be useful.

if you just want to send a simple email tryout tthis code in jsp.

import ="java.util.*,javax.mail.Message,javax.mail.*,javax.mail.internet.*"
 
 
 
Properties props = new Properties();
 
props.put("mail.host", "some.com"); //like yahoo or rediff etc
 
Session mailConnection = Session.getInstance(props, null);
 
Message msg = new MimeMessage(mailConnection);
 
Address bill = new InternetAddress("someone@someone.com","Shop@Comp");
 
Address elliotte = new InternetAddress("someone@yahoo.co.in");
 
msg.setContent("Resistance is futile. You will be assimilated!","text/plain");
 
msg.setFrom(bill);
 
msg.setRecipient(Message.RecipientType.TO, elliotte);
 
msg.setSubject("Congrats You got the mail");
 
Transport.send(msg);


Thank you.

If you want i will give  two link

http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274

Ok every thing is fine but what i understand from the above error

> > >Direct delivery fail! 5.7.1 IRDU-FPI-MAIN-1.NET.MYCOMPANYSDOMAIN.COM Connection not authorized
25/03/2006 15:00:12   Failed to send failure notify message to <kmcghee@campus.ie>!

i think your company has kept certain authorization restriction so just try out this link in which you need to authenticate your self before sending the mail.


http://www.javacommerce.com/destination65673/18274/SendMailUsingAuthentication.java

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.