//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
// where not otherwise noted (c) 2001 http://www.orgdot.com/javaopensource
//____________________________________________________________________________
/* ************************************************************************
* you can copy, use, modify and distribute this code for educational,
* commercial or recreational use. all we ask is that you include
* this copyright notice in the source code you distribute. for
* compiled code, you will need to make accessible this copyright notice
* somewhere in the distribution, and/or via a link on the web.
* there are several reasons for this caveat - the most important being
* that open source is based on one main principle: what you find and use,
* others should also have access to. don't keep it to yourself!
* credit where credit is due! http://www.orgdot.com/javaopensource
*
*
* this software is provided by the author and contributors ``as is'' and
* any express or implied warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed. in no event shall the author or contributors be liable
* for any direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute goods
* or services; loss of use, data, or profits; or business interruption)
* however caused and on any theory of liability, whether in contract, strict
* liability, or tort (including negligence or otherwise) arising in any way
* out of the use of this software, even if advised of the possibility of
* such damage.
*
************************************************************************ */
import java.io.IOException;
import java.io.PrintStream;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.Socket;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class flamail extends HttpServlet
{
final private static String
flash_sent_aok =
"your message was sent!",
flash_sent_nok =
"there was a problem sending your message.\n" +
"i believe you forgot to fill in all the fields.";
final private static String
mailserver = "mail.yourserver.com";
final private static int
mailport = 25;
final private static String[]
domains =
{
"domain1",
"domain2",
"domain3"
},
recipients =
{
"recipient1@yourserver.com",
"recipient2@yourserver.net",
"recipient3@yourserver.org"
};
private String
address,
recipient,
subject,
body;
public void service
(
HttpServletRequest request,
HttpServletResponse response
)
throws ServletException, IOException
{
if
(
setAddress (request.getParameter ( "which_mail") ) &&
setRecipient (request.getParameter ( "which_domain") ) &&
setSubject (request.getParameter ("which_subject") ) &&
setBody
(
request.getParameter ("which_message"),
request.getParameter ( "which_sender"),
request.getRemoteAddr ()
) &&
sendMail ()
)
reply
(
response,
URLEncoder.encode ( flash_sent_aok )
);
else reply
(
response,
URLEncoder.encode ( flash_sent_nok )
);
}
private boolean setAddress
(
String which_mail
)
{
if
(
which_mail == null ||
which_mail.indexOf ("@") < 0 ||
which_mail.indexOf (".") < 0
)
address = "nobody@nowhere.com";
else address = which_mail;
return true;
}
private boolean setRecipient
(
String which_domain
)
{
if (which_domain == null) return false;
for (int i = 0; i < domains.length; i++)
if ( domains[i].equalsIgnoreCase(which_domain) )
{
recipient = recipients[i];
return true;
}
return false;
}
private boolean setSubject
(
String which_subject
)
{
if
(
which_subject == null ||
which_subject.equals ("")
)
subject = "no subject";
else subject = which_subject;
return true;
}
private boolean setBody
(
String which_message,
String which_sender,
String which_ip
)
{
if (which_message == null || which_message.equals ("")) return false;
String sender = ( which_sender == null || which_sender.equals ("") ) ? "anonymous" : which_sender;
body =
which_message + "\n" +
"from: " + sender + "\n" +
"ip trace: " + which_ip;
return true;
}
private boolean sendMail ()
{
try
{
Socket socket = new Socket ( mailserver, mailport );
BufferedInputStream bis = new BufferedInputStream ( socket.getInputStream() );
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
PrintStream ps = new PrintStream ( socket.getOutputStream() );
if
(
readStringLine (bis, baos) &&
writeStringLine (ps, "HELO " + socket.getLocalAddress().getHostName() + "\r\n") &&
readStringLine (bis, baos) &&
writeStringLine (ps, "MAIL FROM: " + address + "\r\n") &&
readStringLine (bis, baos) &&
writeStringLine (ps, "RCPT To: " + recipient + "\r\n") &&
readStringLine (bis, baos) &&
writeStringLine (ps, "DATA\r\n") &&
readStringLine (bis, baos) &&
writeStringLine (ps, "Subject: " + subject + "\r\n") &&
writeStringLine (ps, body + "\r\n") &&
writeStringLine (ps, ".\r\n") &&
writeStringLine (ps, "quit\r\n")
)
{
ps.close ();
bis.close ();
socket.close ();
baos.close ();
}
return true;
}
catch (Throwable t)
{
return false;
}
}
private void reply
(
HttpServletResponse response,
String msg
)
throws IOException
{
response.setContentType("text/html");
PrintStream ps = new PrintStream ( response.getOutputStream() );
if ( writeStringLine (ps, "msg=" + msg) ) ps.close();
}
private boolean writeStringLine
(
PrintStream ps,
String str
)
throws IOException
{
ps.print (str);
ps.flush();
return true;
}
private boolean readStringLine
(
BufferedInputStream in,
ByteArrayOutputStream baos
)
throws IOException
{
baos.reset ();
if (in == null) return false;
in.mark ( in.available() );
int i;
while ( ( i = in.read() ) > - 1)
{
if (i == '\n') return true;
else if (i == '\r')
{
if ( (i = in.read()) == '\n' ) return true;
else if (i == -1 ) return false;
else
{
in.reset ();
in.skip ( baos.size () + 1 );
return true;
}
}
else baos.write (i);
}
in.close();
return true;
}
}