Example of sending mail directly in the servlet

Source: Internet
Author: User
Tags config mail readline socket
servlet| Send mail/**
*class Testemail
* @author Yancheng (j-share.onchina.net)
* @version 1.0.0
*
* Use the write socket to send email.
*/
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Import java.util.*;
Import java.net.*;

Public class Testemail extends HttpServlet
{

private static final String Content_Type = "text/html";


public void init (ServletConfig config) throws servletexception
{
Super.init (config);
}

public void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException
{
Response.setcontenttype (content_type);

PrintWriter out = Response.getwriter ();

Socket s = new socket ("smtp.163.net");

PrintWriter ou = new PrintWriter (S.getoutputstream (), true);
BufferedReader in = new BufferedReader (New InputStreamReader (S.getinputstream ()));

Out.println (" Out.println (" Out.println ("<body>");

String instr = null;
Boolean isOk = false;

try{
ou.println ("helo:smtp.163.net");
Out.println (In.readline ());

Ou.println ("MAIL from:showtop@163.net");
InStr = In.readline ();
Out.println (instr);
if (Instr.startswith ("isOk")) = true;
else Isok=false;

if (isOk) {
Ou.println ("RCPT to:ycshowtop@21cn.com");
InStr = In.readline ();
Out.println (instr);
if (Instr.startswith ("isok=true"));
else IsOk = false;
}

if (isOk) {
Ou.println ("DATA");
InStr = In.readline ();
Out.println (instr);
if (Instr.startswith ("isOk")) = true;
else Isok=false;
}

Ou.println ("This is a test email");
Ou.println (".");
InStr = In.readline ();
Out.println (instr);
if (Instr.startswith ("isok=true"));
else Isok=false;
Ou.println ("QUIT");
S.close ();
}
catch (IOException x)
{
X.printstacktrace (System.err);
}


Out.println ("<p>ok</p>");

Out.println ("</body>}

public void Destroy ()
{
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.