Solved

Java Mail Transport.send(message) issue.....

Posted on 2002-06-01
3
1,516 Views
Last Modified: 2013-11-24
Hi,
I've a problem with Java Mail. In my application I am trying to send a mail with an attachment to the user who clicked the product download link in our website.
The code is developed in Windows 2000 Professional env. Its been successfully tested in the local Solaris also. But, our website is hosted on firstlink.com's webserver. So, the smtpHost and smtpPort nos are changed as per the smtp.firstlink.com configuration settings and hosted in the webserver. Nothing else is modified in my code. But, its throwing NumberFormatException while executing Transport.send();
The code snippet is given below for the review....


 
 public class MailDispatcher{
 
    private String from           = "sales@xxx.com";
    private String to             = "user@xxx.com";
    private String subject        = "License Key file attached";
    private String smtpHost;
    private String smtpPort;
    private String mailMessage    = "Message for client";
    private String signature;
    private String ConfigFile     = "ConfigFile";
    private String DefaultConfigFile   = "download.properties";
    private String fileAttachmentName  = null;
    private static PropertyResourceBundle resource;
    public static final String Mailer = "website.mailer";
    private String logfile;
    private String errorfile;
   
   
   /**
    *  This is the default constructor
    *  Loads   properties from from specified by "ConfigFile" parameter.
    *
    */
    public MailDispatcher() {

         try {

              InputStream inStream = this.getClass().getResourceAsStream("download.PROPERTIES");
              if (inStream == null) {
                  throw new Exception();
              }

              resource = new PropertyResourceBundle(inStream);
              from = resource.getString("from");
              smtpHost = resource.getString("smtpHost");
              smtpPort = resource.getString("smtpPort");
              mailMessage = resource.getString("MailMessage");
              signature = resource.getString("Signature");
              subject = resource.getString("subject");
              logfile = resource.getString("logfile");
              errorfile = resource.getString("errfile");
              fileAttachmentName = resource.getString("attachmentfile");
             
         } catch (Exception ex) {
             Logger.log(Mailer, Logger.Error, new LogMessage(ex));
             ex.printStackTrace();
         }
    }


 
   /**
    * This method is for sending mail to the registered client after product has downloaded successfully.
    * This mail contain the key code for installing the product.
    *
    * @param
    * @return boolean  trueorfalse
    */
    public synchronized boolean sendMail() {

         Properties props = System.getProperties();
         props.put("mail.smtp.host", smtpHost);
         props.put("mail.smtp.port", smtpPort);
         Session session = Session.getInstance(props, null);
         try {              
              // create a message
              message.setFrom(new InternetAddress(getFrom()));
              InternetAddress[] address = {new InternetAddress(to)};
              message.setRecipients(Message.RecipientType.TO, address);
              message.setSubject(subject);
           
              // create and fill the first message part
              MimeBodyPart mbp1 = new MimeBodyPart();
              if (signature != null) {
                mailMessage = mailMessage + "\n\n" + signature;
              }
              mbp1.setText(mailMessage);
              // create the Multipart and its parts to it
              Multipart mp = new MimeMultipart();
              mp.addBodyPart(mbp1);
           
              // attach the file to the message
              MimeBodyPart mbp2 = new MimeBodyPart();
              FileDataSource fds=new FileDataSource(fileAttachmentName);
              mbp2.setDataHandler(new DataHandler(fds));
              mbp2.setFileName("ABCD.doc");//fileAttachmentName
              mp.addBodyPart(mbp2);
           
              // add the Multipart to the message
              message.setContent(mp);
              // set the Date: header
              message.setSentDate(new Date());
              // send the message
              Transport.send(message);
              return true;

         }  catch (Exception e) {
             
              e.printStackTrace();
         }
    }

}


The Exception received is....

java.lang.NumberFormatException: 25
      at java.lang.Integer.parseInt(Integer.java:423)
      at java.lang.Integer.parseInt(Integer.java:463)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:78)
      at javax.mail.Service.connect(Service.java:221)
      at javax.mail.Service.connect(Service.java:127)
      at javax.mail.Service.connect(Service.java:83)
      at javax.mail.Transport.send0(Transport.java:148)
      at javax.mail.Transport.send(Transport.java:73)
      at com.objectfrontier.website.download.util.MailDispatcher.sendMail(MailDispatcher.java:292)
      at download.jsp.page.download.EmailVerifierAction_1._jspService(EmailVerifierAction_1.java:331)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java)
      at org.apache.tomcat.facade.ServletHandler.doService(ServletHandler.java:574)
      at org.apache.tomcat.core.Handler.invoke(Handler.java:322)
      at org.apache.tomcat.core.Handler.service(Handler.java:235)
      at org.apache.tomcat.facade.ServletHandler.service(ServletHandler.java:485)
      at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:917)
      at org.apache.tomcat.core.ContextManager.service(ContextManager.java:833)
      at org.apache.tomcat.modules.server.Ajp13Interceptor.processConnection(Ajp13Interceptor.java:341)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:494)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:516)
      at java.lang.Thread.run(Thread.java:484)

Does anybody have any clue for this???
0
Comment
Question by:Bhogi
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
Have you tried to do this:
smtpPort = resource.getString("smtpPort");

int port = Integer.parseInt(smtpPort);


only to check whether the smtpPort can be parsed?
If this line throw NumberFormatException - I also suggest you to check the length of the smtpPort string.
0
 

Accepted Solution

by:
JavaRam earned 150 total points
Comment Utility
Hi,
The NumberFromatException which u r getting is while converting the String value of the port number into integer. May be there is invalid characters or blank places present while specifying the smtpport no. As u said u r reading the port no from the properties file, after reading it, u can call trim() method to remove the leading/trailing blank spaces.
Hope it helps solving ur issue.
...$Javaram$...
0
 

Expert Comment

by:JavaRam
Comment Utility
Bhogi,
Did u try with my suggestion?? Let us know the trial result.
...$Javaram$...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

11 Experts available now in Live!

Get 1:1 Help Now