• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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
0
kmcghee
Asked:
kmcghee
1 Solution
 
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now