[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1598
  • Last Modified:

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

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
Bhogi
Asked:
Bhogi
  • 2
1 Solution
 
Venci75Commented:
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
 
JavaRamCommented:
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
 
JavaRamCommented:
Bhogi,
Did u try with my suggestion?? Let us know the trial result.
...$Javaram$...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now