package de.spieleck.servlets; import java.net.*; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class Maillet extends HttpServlet { protected Session mailSession = null; public Maillet() { } public void init() throws ServletException { String h; h = getInitParameter("safe"); // XXX h = getInitParameter("mailServer"); if ( h == null || "".equals(h) ) h = "127.0.0.1"; java.util.Properties properties = new java.util.Properties(); properties.put("mail.smtp.host", h ); mailSession = Session.getDefaultInstance( properties, null ); } protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String urls = req.getParameter("url"); if ( urls == null ) urls = "http://"+req.getParameter("hurl"); PrintWriter out = res.getWriter(); URL url; int slashPos = 0; try { slashPos = urls.lastIndexOf("/"); res.setContentType("text/plain"); url = new URL(urls); } catch ( MalformedURLException e ) { out.println("Problem with URL: "+e); return; } String to = req.getParameter("to"); out.println(url); out.println("to "+to+":"); out.flush(); try { MimeMessage msg = new MimeMessage( mailSession ); msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to)); msg.setSubject( url.toString() ); Multipart multi = new MimeMultipart(); msg.setContent(multi); BodyPart part; part = new MimeBodyPart(); part.setText("This is: "+url.toString() ); multi.addBodyPart(part); part = new MimeBodyPart(); out.println(" (Setting Data Source)"); out.flush(); DataSource source = new URLDataSource(url); DataHandler handler = new DataHandler(source); part.setDataHandler(handler); part.setFileName(urls.substring(slashPos+1)); multi.addBodyPart(part); out.println(" (Send)"); out.flush(); Transport.send(msg); } catch ( Exception e ) { out.println("Problem: "+e); } out.println(".. has been send as "+urls.substring(slashPos+1)); } }