• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

在servlet中直接写端口发送邮件的例子

servlet/jsp 搞代码 7年前 (2018-06-18) 154次浏览 已收录 0个评论

/**
*Class TestEmail
*@author yancheng(j-share.onchina.net)
*@version 1.0.0
*
* use write socket to send email.

http://www.gaodaima.com/42003.html在servlet中直接写端口发送邮件的例子

*/
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”,25);

PrintWriter ou = new PrintWriter(s.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

out.println(“<html>”);
out.println(“<head><title>TestEmail</title></head>”);
out.println(“<body>”);

String instr = null;
boolean isOk = false;

try{
ou.println(“HELO:smtp.163.net”);
out.println(in.readLine());

ou.println(“MAIL FROM:[email protected]”);
instr = in.readLine();
out.println(instr);
if(instr.startsWith(“250”)) isOk = true;
else isOk=false;

if(isOk){
ou.println(“RCPT TO:[email protected]”);
instr = in.readLine();
out.println(instr);
if(instr.startsWith(“250”)) isOk=true;
else isOk = false;
}

if(isOk){
ou.println(“DATA”);
instr = in.readLine();
out.println(instr);
if(instr.startsWith(“250”)) isOk = true;
else isOk=false;
}

ou.println(“这是一个测试邮件”);
ou.println(“.”);
instr = in.readLine();
out.println(instr);
if(instr.startsWith(“250”)) 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></html>”);
}

public void destroy()
{
}
}

欢迎大家阅读《在servlet中直接写端口发送邮件的例子》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:在servlet中直接写端口发送邮件的例子

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址