• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Attach a file using java api

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clear to me what you're asking.
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic