Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attach a file using java api

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear to me what you're asking.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic