473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem sending Email using Javamail

274 100+
When I test the application I get follwowing error:
could not connect to smtp host: connection timeout error

can someone please check if I have rigth settings? Where I am wrong here
Is IP and mail host setting ok or not?

Contactus.jsp
Expand|Select|Wrap|Line Numbers
  1. <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN//">
  2. <html>
  3.  
  4. <head>
  5.     <title>Layout template</title>
  6. </head>
  7. <link href="virsar.css" rel="stylesheet" type="text/css" />
  8. <body>
  9. <!------------------------------------------------------------------------------------------------------------------------------->
  10.  
  11.                                                     <table cellspacing=0 cellpadding=0>
  12. <td>------------------------------------------------------------------------------This is dedicated for the Header---------------------------------------------</td>
  13.                                                     </table>
  14. <!------------------------------------------------------------------------------------------------------------------------------->                                                                    
  15.  
  16.  
  17. <table cellspacing=0 cellpadding=0 width=800><!---->
  18.             <td width=170>
  19.  
  20.             Email:         <br>
  21.             Phone: (604)270-7050            <br>
  22.             Toll free: 1-877-DesiORB         <br>
  23.             Fax: (604)270-7062                <br>
  24.  
  25.             <br>
  26.             <br>
  27.             <br>
  28.             Hours of operation:                <br>
  29.             M-F 9:00-5:00 PST                         
  30.  
  31.             </td>
  32.  
  33. <!-----------------------------------------contents ----------------->            
  34.                               <td>
  35.     <br><br><br>
  36.         <h3 align="center">Send us an Email!</h3>
  37.  
  38.          <table>
  39.             <form method="get" action="/virsar/servlet/email.SendMailServlet">
  40.                 <tr>
  41.                     <td>Name : </td><td><input type = "text" name="name" size = 55 MAXLENGTH=50></td>
  42.                 </tr>
  43.                     <tr>
  44.                         <td>Email Address : </td><td><input type = "text" name="emailaddress" size = 55 MAXLENGTH=50></td>
  45.                     </tr>
  46.                         <tr>
  47.                             <td>Subject : </td><td><input type = "text" name="subject" size = 55 MAXLENGTH=50></td>
  48.                         </tr>
  49.                             <tr>
  50.                                 <td>Message / Enquiry : </td><td><textarea rows="10" name="message" cols="50"></textarea></td>
  51.                             </tr>
  52.  
  53.         <tr>
  54.             <td colspan = 2>
  55.                   <p align="right"><input type="submit" value="Email" ><input type="submit" value="Reset" >
  56.               </td>
  57.  
  58.           </tr>
  59.           <tr>
  60.               <td>
  61.                  <% 
  62.  
  63.                    String msg= (String) session.getAttribute("msg");
  64.                    if (msg==null)
  65.                     {msg="";}
  66.                   %>
  67.  
  68.                   <h3 align="center"><%= msg %></h3>
  69.               </td>
  70.           </tr>
  71.  
  72.  
  73. </form>
  74.                             </td>
  75. </table>
  76. <!------------------------------------------------------------------------------------------------------------------------------->
  77.  
  78.                                     <table cellspacing=0 cellpadding=0>
  79.                                     <br><br><br><br>
  80.                                     <td>---------------------------------------------------------------Footer Goes Here----------------------------------------------------------------------</td>
  81.                                     </table>
  82. <!------------------------------------------------------------------------------------------------------------------------------->                
  83. </html>
SendMailServlet.java
Expand|Select|Wrap|Line Numbers
  1. package email;
  2.  
  3. import java.io.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import javax.mail.*;
  7. import util.MailUtil;
  8. //import business.User;
  9.  
  10. public class SendMailServlet extends HttpServlet{
  11.  
  12.     public void doGet(HttpServletRequest request,
  13.                      HttpServletResponse response) 
  14.                      throws IOException, ServletException{    
  15.  
  16.         HttpSession session = request.getSession();
  17.             String to = "farhana.asif@virsarmedia.com";
  18.             String from = "";
  19.             String subject = "";
  20.             String message = "";
  21.             String msg="";
  22.  
  23.         from="virsarmail@gmail.com";
  24.     //    from = request.getParameter("emailaddress"); 
  25.         subject = request.getParameter("emailaddress") + " - " + request.getParameter("name") + ": " + request.getParameter("subject");
  26.         message = request.getParameter("message");
  27.  
  28.         //if user does not have an email address, then use the GCABC's email address as the from address
  29.         if (from.equals(""))
  30.         {
  31.             from = to;
  32.         }
  33.  
  34.       try{
  35.              MailUtil mail = new MailUtil(to, from, subject, message);
  36.              mail.sendMail();
  37.                msg = "Email sent! successfully";
  38.         }
  39.         catch (MessagingException me){
  40.              log("MessagingException: " + from);
  41.              msg = "Email not sent!";
  42.              log(me.toString());
  43.         } 
  44.  
  45.         session.setAttribute("msg", msg);
  46.         RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Contactus.jsp");
  47.  
  48.         dispatcher.forward(request, response);
  49.      }    
  50.  
  51.     public  void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
  52.         doGet(request, response);
  53.     }
  54. }
MailUtil.java
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. package util;
  4.  
  5. import javax.mail.*;
  6. import javax.mail.internet.*;
  7. import java.util.Properties;
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. public class MailUtil{
  12.  
  13. private String sender,recipient1,mailHost,mailPort,mailProtocol,mailUser,mailPassword,mailContent,mailSubject;
  14. Authenticator auth;
  15.  
  16. public MailUtil(String recipient, String sender,
  17.                                 String mailSubject , String mailContent) {
  18.     this.sender = sender;
  19.     recipient1 = recipient;
  20.     mailHost = "209.85.171.83";
  21.     mailPort = "465";
  22.     mailProtocol = "smtp";
  23.     mailUser = "virsarmail@gmail.com";
  24.     mailPassword = "virsarmedia";
  25.     this.mailSubject = mailSubject;
  26.     this.mailContent = mailContent;
  27.     auth = new MyAuthenticator();
  28. }
  29.  
  30. public void sendMail() throws MessagingException{
  31. try {
  32.     Properties props = new Properties();
  33.     props.setProperty("mail.transport.protocol", mailProtocol);
  34.  
  35.     props.put("mail.transport.protocol", mailProtocol);
  36.     props.put("mail.smtp.host", mailHost);
  37.     props.put("mail.smtp.port", mailPort);
  38.     props.put("mail.smtp.user", mailUser);
  39.     props.put("mail.smtp.password", mailPassword);
  40.     props.put( "mail.smtp.auth", "true" );
  41.  
  42. Session mailSession = Session.getInstance(props, auth);
  43. mailSession.setDebug(true);
  44. Transport transport = mailSession.getTransport();
  45.  
  46. MimeMessage message = new MimeMessage(mailSession);
  47. message.setContent(mailContent, "text/plain");
  48. message.setSubject(mailSubject);
  49. message.setSender(new InternetAddress(sender));
  50. message.setFrom(new InternetAddress(sender));
  51. message.addRecipient(Message.RecipientType.TO, new
  52. InternetAddress(recipient1));
  53.  
  54. transport.connect();
  55. transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
  56. transport.close();
  57.  
  58. }
  59. catch(Exception ex) {
  60. System.out.println(ex.toString());
  61. }
  62.  
  63. }
  64.  
  65.  
  66. class MyAuthenticator extends Authenticator {
  67. protected PasswordAuthentication getPasswordAuthentication() {
  68. return new PasswordAuthentication(mailUser, mailPassword);
  69. }
  70. }
  71. }
Jun 24 '08 #1
1 1771
creative1
274 100+
I figured out the problem. Thanks for reading this thread.
Regards
Jun 24 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jared | last post by:
Hello people, I've had major difficulties finding the right Java technology that can be used to make an e-mail applet program. I'm trying to build an applet that would be similar, but much...
0
by: pcouas | last post by:
Hi, Anyone know how reading Outlook or Outlook express contact File from Javamail ? It seems theses files are named .dbx and .vba Thanks Philippe
1
by: Daniel Albisser | last post by:
Hi @ll, I was wondering why I lose the connection to the mail store while retrieving information from it without calling the method store.close()! At the end I found out that the method...
6
by: Mike the Canadian | last post by:
I am having a very strange problem sending email with php. I have two domains. I can send an email to one domain using php but not the other. If I put both email addresses in the mail command only...
1
by: aralvarez_nospam_at_gmail.com change _nospam_at_ | last post by:
I have been trying to register some HL7 schemas into DB2-Express -C v9. After invoking the following command: complete xmlschema "datatypes-base"; it throws me the following error: ...
0
ashsa
by: ashsa | last post by:
Hi all, I was jus working around with the JavaMail API and was stuck at the end with an error from the smtp server saying that Your email message was unable to be sent because your mail...
2
by: reeta | last post by:
Hi, I send the details(firstname,lastname) to database using jsp,and then how will send that values to mailid using javamail. Please help me, I need urgently . thanks in advance.
0
by: sandeepk84 | last post by:
Hi all... I am facing a problem in getting attachments from Yahoo Classic using JavaMail. My program reads the attachments from gmail and Yahoo Beta. But attachments of type pdf or txt from...
1
by: rajujrk | last post by:
Hi all, I am currently doing a project (Send and receive an Email) in Java using the JavaMail API... It ask for SMTP mail server.. How to find the mail server details..
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.