Solved

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

Posted on 2002-06-01
3
1,521 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 65
VB Script to add site to Java Exception List 4 76
Java Loop 6 59
configure dependency in POM for new database 3 25
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

785 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