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
351 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java.lang.UnsupportedClassVersionError 8 373
password protect pdf 11 74
maven j2ee examles 2 49
withoutString  challenge 40 182
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

16 Experts available now in Live!

Get 1:1 Help Now