Solved

Sending Email with JSP (unable to send to certain email addresses e.g. hotmail.com, rocketmail.com etc)

Posted on 2004-10-03
8
347 Views
Last Modified: 2010-08-05
Hi,

I found a JSP coding by kheefatt to send email with JSP in this forum.

The coding is as below:

send_mail.jsp
----------------

<%@ page language="java" import="java.sql.*, java.util.ArrayList" %>
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<html>
<head>
</head>
<body>
<table width="50%" border="0" >
<tr>
<td>
<%
     String emailAdd = request.getParameter("receiver");
     String mailBody = request.getParameter("mailtext");
     boolean mail_error = false;
     String errMsg = "";

     try
     {
          Properties props = new Properties();
          props.put("mail.smtp.host", "127.0.0.1");  //replace 127.0.0.1 with the name u specify in your mail server
          Session s = Session.getInstance(props,null);
          MimeMessage message = new MimeMessage(s);
          InternetAddress from = new InternetAddress(emailAdd);
          message.setFrom(from);
          InternetAddress to = new InternetAddress("kheefatt@yahoo.com"); //your email address
          message.addRecipient(Message.RecipientType.TO, to);
          message.setSubject("Mail Test!");
          message.setText(mailBody);
          Transport.send(message);
     }
     catch (Exception ex)
     {
          String titleMsg = "Error Exception";
          errMsg = ex.getMessage();
          mail_error = true;
     }    
     if (mail_error==false)
          out.write("Thank you for subscribing to our newsletter.<BR>");
     else if (mail_error==true)
     {
          out.write("There's an error in the mail server. Please try again later!<BR>\n");
     }
%>
</td>
</tr>
</table>
</body>
</html>


I replace the 127.0.0.1 in props.put("mail.smtp.host", "127.0.0.1") with the address I usually put in the SMTP serve section of my Outlook Express (mail.mycompany.com). I was able to sent to all mycompany.com accounts (i.e xxx@mycompany.com) but was unable to send other account such as xxx@hotmail. What did I do wrong?
FYI, the mail server is provided by the hosting company. We rented a webspace with a hosting company, and the package include email accounts.
0
Comment
Question by:minevra
8 Comments
 
LVL 11

Expert Comment

by:raj3060
ID: 12212685
you probably need to putr the IP address with the port
i.e.
props.put("mail.smtp.host", "127.0.0.1:8080")
0
 

Expert Comment

by:kheefatt
ID: 12214825
I think it is sent to the junk mail folder. Check your junk mail folder and see.

It is impossible it works on all your mycompany.com accounts but not to hotmail. Try to disable your junk mail filter and try again. Good luck!
0
 
LVL 1

Author Comment

by:minevra
ID: 12215108
There is nothing wrong with hotmail. There is actualy error in the program when I try to sent to hotmail.

Here is the error message:

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Sending failed;
  nested exception is:
      class javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
      class javax.mail.SendFailedException: 550 not local host roketmail.com, not a gateway

      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:682)
      org.apache.jsp.testmail_jsp._jspService(testmail_jsp.java:74)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:293)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

javax.mail.SendFailedException: Sending failed;
  nested exception is:
      class javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
      class javax.mail.SendFailedException: 550 not local host roketmail.com, not a gateway

      javax.mail.Transport.send0(Transport.java:218)
      javax.mail.Transport.send(Transport.java:80)
      org.apache.jsp.testmail_jsp._jspService(testmail_jsp.java:64)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:293)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

0
 

Expert Comment

by:kheefatt
ID: 12215119
contact your hosting company and asked them bout the specified ip and port
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:minevra
ID: 12215216
hi kheefatt,

I found out that I have to put in my username and password in order to send outside our own company account. How can I do that?

Can u show me the code?

thanks
0
 

Expert Comment

by:kheefatt
ID: 12215234
I think u haf the application did wrongly. U dont need to put in any username and password in order to send a mail through a mail server. U download a free smtp mail server and try it out on yur local pc first.
props.put("mail.smtp.host", "127.0.0.1") <-- u put in the IP or the name of the mail server, not your email account.
0
 
LVL 1

Author Comment

by:minevra
ID: 12215250
This is what my colleague told me. She did a similar program before and she also face the same problem until still put in username and password (I can't use her codes because it's very different from mine). My hosting company purposely set their servers to require authentication to sent mail. This is to prevent spamming.
0
 
LVL 19

Accepted Solution

by:
ramazanyich earned 125 total points
ID: 12215820
in JavaMail API you can set username and password for SMTP authentication.
Check following code http://www.javacommerce.com/articles/SendMailUsingAuthentication.java
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
loop through list of objects 14 186
Stuck in Bootstrap WysiHtml Editor 3 36
getting Http 404 error in jsp forms 8 72
equalIsNot  challenge 43 117
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now