aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Attach a file using java api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Attach a file using java api" Watch "Attach a file using java api" New topic
Author

Attach a file using java api

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
Attach a file using java api even it has to work in remote machine the following is the code i am using it is working in my local machine with file attachment please suggest what i have to do?

MimeMessage msg = new MimeMessage(session);
InternetAddress[] TheAddresses = InternetAddress.parse(sFrommailId);
msg.addFrom(TheAddresses);
TheAddresses = InternetAddress.parse(sTo);
msg.addRecipients(Message.RecipientType.TO,sTo);
msg.setSubject(sSubject);
MimeBodyPart mbp1 = new MimeBodyPart();
//mbp1.setText(sBody);
mbp1.setContent(sBody,"text/html");

MimeBodyPart mbp2 = new MimeBodyPart();

if (null != sCC)
{
TheAddresses = InternetAddress.parse(sCC);
msg.addRecipients(Message.RecipientType.CC,TheAddresses);
}
// attach the file to the message
System.out.println("Sending mail1234....");

FileDataSource fds = new FileDataSource(new File(sFileName));

mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
Transport.send(msg);


Can any one give proper response for this it is so urgent!!! thanks in advance


SCJP5 and SCWCD1.5
Think Twice Act Wise...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's not clear to me what you're asking.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Your mention of "local" and "remote" machines suggests to me that you are trying to attach a file which is on a different computer. I would also guess that the different computer has sent a request to the computer where this code is running, asking it to send a file via e-mail.

If all of my guesses are right, then the computer sending the request will probably have to send the file along with the request, and the computer running this code will have to receive that file and store it somewhere locally, so it can then attach it to the e-mail.

However that's a lot of guesses. You could help things along by describing specifically what you are trying to do, instead of posting a general question about local and remote machines.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
Your Guess is right.

There are two machines one is server and one is client. when i am trying to attach the file in server it is working. the mail attachment is going fine. whenever i am trying to attach the file thro client machine i cant attach it is saying that file not found exception. the bold one indicates that i am attaching a file. please suggest me what i need to do?



--------------------------------------------------------------------------------

Attach a file using java api even it has to work in remote machine the following is the code i am using it is working in my local machine with file attachment please suggest what i have to do?

MimeMessage msg = new MimeMessage(session);
InternetAddress[] TheAddresses = InternetAddress.parse(sFrommailId);
msg.addFrom(TheAddresses);
TheAddresses = InternetAddress.parse(sTo);
msg.addRecipients(Message.RecipientType.TO,sTo);
msg.setSubject(sSubject);
MimeBodyPart mbp1 = new MimeBodyPart();
//mbp1.setText(sBody);
mbp1.setContent(sBody,"text/html");

MimeBodyPart mbp2 = new MimeBodyPart();

if (null != sCC)
{
TheAddresses = InternetAddress.parse(sCC);
msg.addRecipients(Message.RecipientType.CC,TheAddresses);
}
// attach the file to the message
System.out.println("Sending mail1234....");

FileDataSource fds = new FileDataSource(new File(sFileName));

mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); // create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
Transport.send(msg);


Can any one give proper response for this it is so urgent!!! thanks in advance


[/code]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Ganeshkumar cheekati wrote:it is so urgent!!!

Please EaseUp.

The client will need to upload the file to the server first. Then it is located on the server, probably only temporarily, and the server can access it. The process would be this:
- client uploads file to the server
- server attaches the file to the email
- server sends the email
- server deletes the file since it's no longer needed


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attach a file using java api