aspose file tools*
The moose likes Servlets and the fly likes sending attachment with mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sending attachment with mail " Watch "sending attachment with mail " New topic
Author

sending attachment with mail

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending attachment with mail