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
352 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript onclick event 4 107
tomcat users xml 7 117
problem with  angularjs dropdwon search at object property (name) level 2 99
login form jsp example 2 24
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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