Bhogi
asked on
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().getResourc eAsStream( "download. PROPERTIES ");
if (inStream == null) {
throw new Exception();
}
resource = new PropertyResourceBundle(inS tream);
from = resource.getString("from") ;
smtpHost = resource.getString("smtpHo st");
smtpPort = resource.getString("smtpPo rt");
mailMessage = resource.getString("MailMe ssage");
signature = resource.getString("Signat ure");
subject = resource.getString("subjec t");
logfile = resource.getString("logfil e");
errorfile = resource.getString("errfil e");
fileAttachmentName = resource.getString("attach mentfile") ;
} 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(Mess age.Recipi entType.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(fileAttachm entName);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName("ABCD.doc ");//fileA ttachmentN ame
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.NumberFormatExce ption: 25
at java.lang.Integer.parseInt (Integer.j ava:423)
at java.lang.Integer.parseInt (Integer.j ava:463)
at com.sun.mail.smtp.SMTPTran sport.prot ocolConnec t(SMTPTran sport.java :78)
at javax.mail.Service.connect (Service.j ava:221)
at javax.mail.Service.connect (Service.j ava:127)
at javax.mail.Service.connect (Service.j ava:83)
at javax.mail.Transport.send0 (Transport .java:148)
at javax.mail.Transport.send( Transport. java:73)
at com.objectfrontier.website .download. util.MailD ispatcher. sendMail(M ailDispatc her.java:2 92)
at download.jsp.page.download .EmailVeri fierAction _1._jspSer vice(Email VerifierAc tion_1.jav a:331)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:119 )
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java)
at org.apache.tomcat.facade.S ervletHand ler.doServ ice(Servle tHandler.j ava:574)
at org.apache.tomcat.core.Han dler.invok e(Handler. java:322)
at org.apache.tomcat.core.Han dler.servi ce(Handler .java:235)
at org.apache.tomcat.facade.S ervletHand ler.servic e(ServletH andler.jav a:485)
at org.apache.tomcat.core.Con textManage r.internal Service(Co ntextManag er.java:91 7)
at org.apache.tomcat.core.Con textManage r.service( ContextMan ager.java: 833)
at org.apache.tomcat.modules. server.Ajp 13Intercep tor.proces sConnectio n(Ajp13Int erceptor.j ava:341)
at org.apache.tomcat.util.net .TcpWorker Thread.run It(PoolTcp Endpoint.j ava:494)
at org.apache.tomcat.util.thr eads.Threa dPool$Cont rolRunnabl e.run(Thre adPool.jav a:516)
at java.lang.Thread.run(Threa d.java:484 )
Does anybody have any clue for this???
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().getResourc
if (inStream == null) {
throw new Exception();
}
resource = new PropertyResourceBundle(inS
from = resource.getString("from")
smtpHost = resource.getString("smtpHo
smtpPort = resource.getString("smtpPo
mailMessage = resource.getString("MailMe
signature = resource.getString("Signat
subject = resource.getString("subjec
logfile = resource.getString("logfil
errorfile = resource.getString("errfil
fileAttachmentName = resource.getString("attach
} 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"
props.put("mail.smtp.port"
Session session = Session.getInstance(props,
try {
// create a message
message.setFrom(new InternetAddress(getFrom())
InternetAddress[] address = {new InternetAddress(to)};
message.setRecipients(Mess
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(fileAttachm
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName("ABCD.doc
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.NumberFormatExce
at java.lang.Integer.parseInt
at java.lang.Integer.parseInt
at com.sun.mail.smtp.SMTPTran
at javax.mail.Service.connect
at javax.mail.Service.connect
at javax.mail.Service.connect
at javax.mail.Transport.send0
at javax.mail.Transport.send(
at com.objectfrontier.website
at download.jsp.page.download
at org.apache.jasper.runtime.
at javax.servlet.http.HttpSer
at org.apache.tomcat.facade.S
at org.apache.tomcat.core.Han
at org.apache.tomcat.core.Han
at org.apache.tomcat.facade.S
at org.apache.tomcat.core.Con
at org.apache.tomcat.core.Con
at org.apache.tomcat.modules.
at org.apache.tomcat.util.net
at org.apache.tomcat.util.thr
at java.lang.Thread.run(Threa
Does anybody have any clue for this???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bhogi,
Did u try with my suggestion?? Let us know the trial result.
...$Javaram$...
Did u try with my suggestion?? Let us know the trial result.
...$Javaram$...
smtpPort = resource.getString("smtpPo
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.