Hello,
Javamail is really an impressive piece of software.. but i've a little
cosmetic problem
When I mail files the sent file is named with the full path included.
I mean that a myfile.doc file that is stored in
C:\mydocs\mybeautifulldirectory\stuff\foo
will be received named :
Cmydocsmybeautifulldirectorystufffoomyfile.doc
here is my code:
note : the "file" parameter sent by the HTML form contains the whole path
of my file. If I don't do that JSWDK 1.0 answer that it can't access the file
//----- START -------
package lse.pictoris.intramkt;
//JDK
import java.io.*;
import java.util.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
// API Servlet
import javax.servlet.*;
import javax.servlet.http.*;
//API Javamail
import javax.mail.*;
import javax.mail.internet.*;
// JAF
import javax.activation.*;
public class FichierEnvoiMail extends HttpServlet implements Runnable {
Thread postier;
String subject = "[Postier automatique] Voici le fichier que vous avez
demande";
String to = null;
String host = "192.168.0.2";
String filename= null;
String cc = null;
String from = "intranet marketing developpement
<[EMAIL PROTECTED]>";
String mailer = "Intranet Marketing Developpement";
String bcc = "[EMAIL PROTECTED]";
public void init (ServletConfig config) throws ServletException {
super.init(config);
postier = new Thread(this);
postier.setPriority(Thread.NORM_PRIORITY);
postier.start();
}
public void run() {
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props, null);
String body = "Bonjour [... A MESSAGE TELLING THAT THE REQUESTED FILE
IS ATTACHED...]....";
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds=new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(filename);
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
//set the mailer header
msg.setHeader("X-Mailer", mailer);
// send the message
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
//========================================================================
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
to = req.getParameter ("email");
filename = req.getParameter ("file");
Cookie emailCookie = new Cookie ("postier", to);
res.addCookie(emailCookie);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
[..HTML DISPLAYING FOR THE WEB USER..]
out.println("</body></html>");
out.close();
}
//========================================================================
public void destroy(){
postier.stop();
System.gc();
}
}
//----- END ---------
+------------------------------------------------+
| Luc Saint-Elie |
| Development and marketing manager |
| Pictoris - Paris - France |
| http://www.pictoris.com |
| email : [EMAIL PROTECTED] |
+------------------------------------------------+
| Java Server Side Open Source technologies |
| http://www.interpasnet.com/JSS |
+------------------------------------------------+
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html