This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic