• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sending attachment with mail

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i am deloping a mail client using java mail API
and servlets.
The code i wriiten woking fine when i am using the same machine as client on which i am running the server(JSWDK) but when i use
different machine as client it searches file which i browse for attachment on server machine instead of client machine and throws no such file found excetion.
given below is code written for sending message:
private void doSend(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException, MessagingException
{
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String from = request.getParameter("from");
String to = request.getParameter("to");
String cc = request.getParameter("cc");
String bcc = request.getParameter("bcc");
String subject = request.getParameter("subject");

if(subject.equals(""))
{
System.out.println("empty");
subject="none";
}

String text = request.getParameter("text");
try{
//Construc message new
MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
message.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
message.setSubject(subject);

//adding file to the message
MimeBodyPart mb1=new MimeBodyPart();
MimeMultipart mp=new MimeMultipart();

mb1.setContent(text,"text/plain");
mp.addBodyPart(mb1);
System.out.println("In dosend");

StringTokenizer stoken = new StringTokenizer(new String(browsestr),";");
while (stoken.hasMoreTokens())
{
System.out.println("inside stringtoken");
MimeBodyPart mbp2=new MimeBodyPart();
File file = new File(stoken.nextToken());

System.out.println("contents are written");
FileDataSource fds=new FileDataSource(file); //myBrowse
mbp2.setFileName(file.getName());
mbp2.setDisposition(Part.ATTACHMENT);
mbp2.setDescription("Attached file: " + file.getName());
mbp2.setDataHandler(new DataHandler(fds));
System.out.println("in while of attach");
mp.addBodyPart(mbp2);
}
System.out.println("after stringtoken");


message.setContent(mp);
System.out.println("before transport");
//Send the message
Transport.send(message);
}
bhuwan
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic