How to make file attchment in java?

hi
Here is the code i am using for mail attachment without attachment it is working fine
package com.nihon.login;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
public class Mail {
* @param args
private static String host = null;
private static String from = null;
private static String to = null;
private static String subject = null;
private static String content = null;
private static String filename = "D:\\Users
entry.pdf";
public static void send(String fromAddress,String toAddress,String subject, String mailContent) throws Exception{
send(fromAddress,toAddress,null,subject,mailContent);
public static void send(String fromAddress,String toAddress, String ccAddress,String subject, String mailContent) throws Exception {
ResourceBundle rb = ResourceBundle.getBundle("com.nihon.login.Application");
MailInfo mail = new MailInfo();
mail.setMailServer(rb.getString("nihon.mail.server"));
if(fromAddress!=null){
mail.setFromAddress(fromAddress);
}else{
mail.setFromAddress(rb.getString("nihon.mail.fromID"));
host = mail.getMailServer();
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(mail.getFromAddress()));
// Set the to address
if(toAddress!=null)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
if(ccAddress!=null){
message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccAddress));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(mailContent);
For attachment
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// Send message
Transport.send(message);
// Method to instantiate once
public static Mail getInstance() {
if (instance == null) {
instance = new Mail();
return new Mail();
// Construtctor
protected Mail() {
private static Mail instance = null;
But now i add some code for attachment after that i got the exception in below
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: No content
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at com.nihon.login.Mail.send(Mail.java:104)
at com.nihon.login.Mail.send(Mail.java:47)
at com.nihon.login.Mailsender.sendMail(Mailsender.java:47)
at com.nihon.login.Mailsender.attributeAdded(Mailsender.java:19)
at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1334)
at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1243)
at org.apache.catalina.session.StandardSessionFacade.setAttribute(StandardSessionFacade.java:130)
at com.nihon.track.OnsitePlacement.doProcessRequest(OnsitePlacement.java:347)
at com.nihon.track.OnsitePlacement.doPost(OnsitePlacement.java:420)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
at java.lang.Thread.run(Unknown Source)
How can i solve this problem.
Thanks in advance.......

Actually the file name is in the path of D:/users/entry.pdf it will be in my local machine and i hot coded the file name.

Similar Messages

  • How to make destop application using java

    how to make destop application using java
    i mean to say when we double click on the exe file the program should lounch the application
    for windows

    when i double click ii got the msg
    Failed to lad main class manifest attribute from
    x/x/xyz.jar
    what exatly it meansIt means that your Manifest.MF for the JAR file should contain an attribute called Main-Class pointing to a class containing the main() method.
    Something like this should be present in META-INF/Manifest.MF :
    Main-Class: MyApplicationMain

  • How to make File Dialog in different color

    Hi to all,
    How to make File Dialog to appear in orange color.
    Regards
    khiz_eng

    I would like to know this also.....but it appears that there is not much you can do with an AWT FileDialog (not even moving it to the center of the screen).
    sigh....
    V.V.

  • How to make "Levels" in simple java game

    I just wanted to know if anybody knew how to make "Levels" in a java game. In my case, it is to change two polygons that are used in the background. I think you have to use an array of some kind, but i dont really know.
    Here is my source, the polygons are by the massive ///////// areas.
    I cut out the majority of the program, because it was too long.
    public class collision extends Applet implements MouseListener,MouseMotionListener
        private  Image rickImage,mazeImage;
       Image Buffer;
       Graphics gBuffer;
       int x, y;
       int[] LeftWallX = {0,204,204,149,149,162,162,243,260,235,259,232,260,230,207,207,0};
       int[] LeftWallY = {500,499,402,402,341,324,227,191,157,141,135,123,116,109,86,1,1};
       //int[] PlayAreaX = {204,204,149,149,162,162,243,260,235,259,232,260,230,207,207,263,263,245,282,262,274,257,273,250,184,184,170,170,208,216,216};
      // int[] PlayAreaY = {499,402,402,341,324,227,191,157,141,135,123,116,109,86,1,1,86,103,115,129,138,147,155,201,230,323,340,384,384,402,499};
       int[] RightWallX = {500,500,263,263,245,282,262,274,257,273,250,184,184,170,170,208,216,216};
       int[] RightWallY = {500,0,1,86,103,115,129,138,147,155,201,230,323,340,384,384,402,499};
       boolean mouseInside, collide;
       boolean rolled = false;
       boolean msg = true;
       int sX=204,sY=490,sW=12,sH=9;
       //Declare the rectangles
       Rectangle  movingRect,finshBloc,startBloc,oopsBloc;
       //Declare the polygons
       Polygon leftWall,playerArea,rightWall;
            ///Initiate
            public void init()
                 rickImage = getImage(getDocumentBase(), "rick.jpg");
                 mazeImage = getImage(getDocumentBase(), "maze1.jpg");
                 collide=false;
                 Buffer=createImage(getSize().width,getSize().height);
                 gBuffer=Buffer.getGraphics();
                 rightWall=new Polygon(RightWallX,RightWallY,18);
                 playerArea= new Polygon(PlayAreaX,PlayAreaY,31);
                 leftWall= new Polygon(LeftWallX,LeftWallY,17);
            public void paint(Graphics g)
                 drawStuff();
                 g.drawImage (Buffer,0,0, this);
    */

    I'm not exactly sure in your case what you are trying to accomplish. If all you want to do is make new polygons for your levels then you will simply need a Vector of type level (or polygon). Store multiple levels/polygons in that vector. This can be done many ways, probably the most efficient way would be to create a class Level of sorts and create each level object from there. This way you have all your levels stored into that vector.
    I have made programs where the levels/mazes are randomly generated based on certain parameters (this way you would not need to define any specific level). This can be done inside the Level class and added to the vector so that when a level is randomly generated there are literally infinite possibilities. I would suggest posting all your code or at least a breakdown UML diagram of what is going on in your entire program.

  • How To Make File Association Changes Permenant

    I'm having a problem with file associations reverting back every time I turn of my computer.
    I'm using Finder "change all" function to change the file association.  It works until I turn off my computer.  Once rebooted file associations return.
    It's a real hassle.  For some reason MP4 files keep getting associated with Windows media player. Everytime I try to open one, parallalels loads up windows. 
    Any advise?

    ekohanowich wrote:
    how to make file able to open on pc
    I assume you mean Windows Computer.
    in Keynote:   file > export > PowerPoint
    Each Windows machine will need  Powerpoint or PowerPoint Viewer installed.

  • How to make file able to open on pc

    how to make file able to open on pc

    ekohanowich wrote:
    how to make file able to open on pc
    I assume you mean Windows Computer.
    in Keynote:   file > export > PowerPoint
    Each Windows machine will need  Powerpoint or PowerPoint Viewer installed.

  • How to make plsql parser around java method

    can someone tell me how can i move Jdeveloper java method into oracle DB (how to make plsql parser around java method). Is this possible becouse in google i can not find any good example how to make this.

    Hi,
    I think you are talking about Java Stored Procedure (?)
    Look here, http://download.oracle.com/docs/cd/E11882_01/java.112/e10588/toc.htm
    Regards
    Peter

  • How to make file associations in my java apps

    I am making a Graph maker in Java. Just give x = 1,2,3,4,5 and corresponding y = f(x) and get the discreate graph.
    The graph can then be saved as a file of extention .grf (say) on the hard disk, and can be opened in my graph maker software for editing, viewing e.t.c.
    Now my problem is that I want that when I dubble click on a file xyz.grf my graph maker should open automatically with this file opened in it, like other applications on windows say Notepad - dubble click on xyz.txt it will automatically launch Notepade with this file.
    In other way I want that any .grf file gets associated to my graf maker.
    Please help me and tell me the way, how should I proceed and how can I do this.

    aashishjava wrote:
    Yes Yes Yes It is 99% what I wanted to have. Thanks a lot ....You're welcome.
    But now I want some thing more,(hand to forehead) They always want more. ;)
    (a) There is a splash screen in my app. but JNLP always show its own splash screen, ...Webstart splash screens work differently to those of plain Jar files (which is unfortunate). A webstart splash screen has to be not included in the Jar, but available as a separate resource. The splash is defined in the JNLP launch file. For further details see the [JNLP File Syntax|http://java.sun.com/j2se/1.4.2/docs/guide/jws/developersguide/syntax.html].
    <quote>
    The optional kind="splash" attribute may be used in an icon element to indicate that the image is to be used as a "splash" screen during the launch of an application. If the JNLP file does not contain an icon element with a kind="splash" attribute, but does contain another icon tag, Java Web Start will display a splash screen consisting of the image specified by the icon element on the left and the application's title and vendor on the right.
    The first time an application is launched following the addition or modification of the icon element in the JNLP file, the old splash image will still be displayed. The new splash image will appear on the second and subsequent launches of the application.
    </quote>
    The 'ignored first time' is because the webstart client is more focused on getting the app. downloaded, cached and launched, than showing splashes. Once the app. is on-screen, it will download the splash in the background.
    One other 'gotcha' of splash screens is that the JNLP file must define a href in the JNLP element for the splash to be used. The webstart client assumes that a JNLP with no href is dynamically generated, and will not be the same twice. Because of that it ignores the splash!
    Edit 1:
    Unfortunately, I do not have any direct examples of using splash screens A splash is usually on my 'To Do' list for projects, but it is a very low priority.
    Edited by: AndrewThompson64 on Dec 10, 2009 11:35 AM

  • How to make installer of my java application for linux

    I want to make an installer for my java application, I am using Install any where trial version its create an installer but when i installed n execute it, it display that
    "unable to locate main class, class must be public n having a public static void main(....) LAX."
    while making the installer I specifically mention the main class.....
    I tried a lot but getting same error again n again,
    Any one has any idea, what went wrong or what should I do to make Installer by using any good tool.
    Thanks for every Thing.

    After making the installar using InstallAnywhere, when i run the installer it shows the following error....
    Unable to locate the application's 'main' class. The class 'wekna.gui.explorer.LoginScreen' must be public and have a 'public static void main(String[])' method. (LAX)
    Unable to Launch Java Application: Unable to locate the application's 'main' class. The class 'weka.gui.explorer.LoginScreen' must be public and have a 'public static void main(String[])' method. (LAX)
    my main class has syntax public static void main(...) because otherwise java will generate error,
    my application is running well with make file but I want to make an installer......

  • How to make a table in java without using GUI??

    how can i make a table in java without usinf GUI, just simple codes??
    NAME ID NUMBER ADDRESS STATUS

    If you simply want to store them internally, you don't want to use a table.
    Make a simple class with properties id, name, number, address, status. Then create a Hashtable that indexes instances of the class by id or some other property.

  • How to make Connection Pooling in JAVA

    Dear Members
    I want to know that how can i make connection pooling in java and also how can i use that pool in my jsp/servlet application.
    Plz Describe in Detail.
    Thanks
    Vasim

    vasim_saiyad2000 wrote:
    Dear Members
    I want to know that how can i make connection pooling in java and also how can i use that pool in my jsp/servlet application.
    Plz Describe in Detail.
    Thanks
    VasimAs the previous poster is trying to suggest, the server you use will have datasource and connection pooling support. so look up in the manual how to set it up, or do a google search. For example if you use Tomcat, look here:
    http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

  • How to make files read only

    I want to make files read only.  Any workaround is welcomed.

    t quinn wrote:
    ElMagoElGato,
    I am glad to give some help but I am still confused because we are having different experiences. I have been able to lock Tables on my mac and have that lock respected in iOS for a long while now. Easy to unlock, impossible to relock.  With the most recent upgrade I can lock tables after tapping the upper left dot to select the whole table and going to "arrange" in the paintbrush.
    When you say worksheet are you meaning the entire file? When you say file lock are you meaning password protection? You can see I am confused.
    quinn
    Based on what ElMagoElGato has described, password protecting would be of no value to him. Once the file is accessed, it is in edit mode. Unless I'm really missing something.
    Files I have imported from Mac iworks apps with elements locked stay locked for me in iOS. Not sure what the problem is.

  • How to make a scheduler in Java

    I am interested in make a scheduler in java that can run my own services according to schedule.Can any one help me for this.Thanks in advance.
    Rizwan

    Follow this code Hope this will help u
    import java.util.Timer;
    import java.util.TimerTask;
    * Simple demo that uses java.util.Timer to schedule a task to execute
    * once 5 seconds have passed.
    public class Reminder {
    Timer timer;
    public Reminder(int seconds) {
    timer = new Timer();
    timer.schedule(new RemindTask(), seconds*1000);
    class RemindTask extends TimerTask {
    public void run() {
    System.out.println("Time's up!");
    timer.cancel(); //Terminate the timer thread
    public static void main(String args[]) {
    System.out.println("About to schedule task.");
    new Reminder(5);
    System.out.println("Task scheduled.");
    When you run the example, you first see this:
    Task scheduled.
    Five seconds later, you see this:
    Time's up!
    Chatrapathy

  • How to make file versioned automatically when it is created

    I am wondering it is possible to use the server side override to make file versioned when it is created. Does anyone have code to do it? Thanks a lot,
    Jean

    It might be possible. The difficult part is moving all the object creation code into an override. I did it by creating a new parser that always creates new documents as versioned (among other things) and have found that to work very well.

  • How to make standalone program in java?

    I have try use jar and it work. but how to access a folder that in the jar file?
    example:
    ------files---------------------------------------------------------------
    pic ( folder that storage images)
    mainClass.txt (set where is the "main" class)
    myclass.class
    main.class
    and i run in command line:
    jar cmf mainClass.txt run.jar *.*
    after that its created run.jar that store all files that i want
    In main.class i get a images from a pic folder.
    So how i get images from a pic folder after i make a standalone jar file ( get a path of image (up one folder of my main.class folder) in jar file where my main.clss is in the jar file too ) ?
    thx..

    Actually i access my image from other class, class that not contain main class, let call myclass, myclass isn't contain main class
    i hava set path
    "ImageIcon icon" in the myclass.class
    ImageIcon icon = new ImageIcon(myclass.class.getClassLoader().getResource(path));its error:
    "Could not find the main class. Program will exit"
    i try:
    ImageIcon icon = new ImageIcon(main.class.getClassLoader().getResource(path));but the same error too..
    Edited by: sayunara on Apr 18, 2008 2:05 AM

Maybe you are looking for

  • Error when trying to create a Composite Application Services project

    Hi Experts, I am facing a problem while trying to create a project using NWDS 7.0 SP 13 New Development Component Project-> Composite  Application Services then pressing finish, it throws an error as follows "Generation can not completed due to one o

  • How to Switch Off Speech Recognition

    I activated speech recognition just to see what it can do and got the round microphone icon in my screen. As I press Esc I can give commands. I want to get rid of this round thing in my screen though and switch off Speech Recognition completely becau

  • MIRO / F-53 in IDOC process

    we have a requirement where in we need to post invoice using idoc_input_invoic_mrm FM (coming as INVOIC02 IDoc) and once posted successfully, immediately clear the document using F-53.. What is the best approach to do this? 1) clone IDOC FM and call

  • Can't see detail on Google Docs

    Hello.  First time I haven't brought a Mac and although I am trying to be positive and realistic about the level of service I will get from the Touchsmart I must say it hasn't been a great experience so far.  Within a month of using it, it had to be

  • Required in argument CSVRowCount not supplied for BulkOpImportCreateRequest

    SBL-DAT-00125 error code while using sample demo files in import section trying to import from account.xls into the CRM On Demand. Please advice.