Solved

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

Posted on 2002-06-01
3
1,519 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
ID: 7050580
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
ID: 7050744
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
ID: 7055866
Bhogi,
Did u try with my suggestion?? Let us know the trial result.
...$Javaram$...
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
groupSumClump challenge 9 98
What is the latest versions eclipse neon 2 138
JList custom Cell Renderer refresh 15 53
servlet doXXX methods 3 31
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

948 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

21 Experts available now in Live!

Get 1:1 Help Now