Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

IOException while sending the mail with attachment

807606May 23 2007 — edited May 23 2007
Hi All,
Please help me regading my problem,
Here is my code to send message with attachment.
Without attachment it sent mail successfully.
In my project i used servlet and the sending code is as part of the applet.
Here the parameter file is getting from the server :
\Spaceco\SpaceCoReports\043628-05-23-2007(06-54-39PM).pdf
Spaceco is my project name.

I got the exception while tramsport the mssage.
Transport.send(msg);
public void postMail( String smtpHost, String recipients[], String subject,
                        String message , File file, String from) throws MessagingException
    {
        boolean debug = false;

        //Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);

        session.setDebug(debug);
	// create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
	
	InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)        
            addressTo[i] = new InternetAddress(recipients);

msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setSentDate(new Date());

if(!file.equals(""))
{
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(message);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName((new File(file.toString())).getName());

multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
}
else msg.setContent(message, "text/plain");
Transport.send(msg); //////////-----------------(1)
}


Here is my exception :
javax.mail.MessagingException: IOException while sending message
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:595)
	at javax.mail.Transport.send0(Transport.java:169)
	at javax.mail.Transport.send(Transport.java:98)
	at com.spaceco.JSpaceCoDynamics.onlineApp.sales.SendMail.postMail(SendMail.java:448)
	at com.spaceco.JSpaceCoDynamics.onlineApp.sales.SendMail.okButtonActionPerformed(SendMail.java:369)
	at com.spaceco.JSpaceCoDynamics.onlineApp.sales.SendMail.access$200(SendMail.java:57)
	at com.spaceco.JSpaceCoDynamics.onlineApp.sales.SendMail$2.actionPerformed(SendMail.java:314)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: \Spaceco\SpaceCoReports\043628-05-23-2007(06-54-39PM).pdf (The system cannot find the path specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(Unknown Source)
	at javax.activation.FileDataSource.getInputStream(FileDataSource.java:80)
	at javax.activation.DataHandler.writeTo(DataHandler.java:287)
	at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1248)
	at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:747)
	at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
	at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
	at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:839)
	at javax.activation.DataHandler.writeTo(DataHandler.java:295)
	at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1248)
	at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1673)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:555)
	... 31 more
I really struggled with this issue,
Please can any one guide me!

Thanks,
Kiran
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 20 2007
Added on May 23 2007
4 comments
8,590 views