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
354 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:kheefatt
ID: 12215119
contact your hosting company and asked them bout the specified ip and port
0
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
J2SE 5.0 metadata 1 67
console vs log file 16 83
struts hibernate integration example 28 151
struts spring hibernate example 12 137
Original post  on Monitis Blog. Web performance monitoring is broken into two camps: passive and active. Passive monitoring is defined as looking at real-world historical performance by monitoring actual log-ins, site hits, clicks, requests for…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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