用sun.net.smtp类发送EMAIL的一点问题

demon007 2001-07-19 10:10:54
package myservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import sun.net.smtp.*;

public class SmtpC extends HttpServlet {
private static final String CONTENT_TYPE = "text/html;charset=gb2312";
private static final String MAIL_FROM = "from";
private static final String MAIL_SUBJECT = "subject";
private static final String MAIL_BODY = "body";

private static final String MAIL_TO = "to";
private static final String MAIL_HOST = "host";

/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>SmtpC</title></head>");
out.println("<body>");
out.println("<form name=\"" + request.getRequestURI() + "\" method=\"post\"");
out.println("<p>from:<input type=text size=20 name=\"" + MAIL_FROM + "\"></p>");
out.println("<p>subject:<input type=text size=30 name=\"" + MAIL_SUBJECT + "\"></p>");
out.println("<p>body:<textarea rowspan=3 colspan=30 name=\"" + MAIL_BODY + "\"></textarea></p>");
out.println("<p>to:<input type=text size=20 name=\"" + MAIL_TO + "\"></p>");
out.println("<p>mail host:<input type=text size=20 name=\"" + MAIL_HOST + "\"></p>");
out.println("<p><input type=submit></p>");
out.println("</form>");
out.println("</body></html>");
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String host = request.getParameter(MAIL_HOST);
String from = request.getParameter(MAIL_FROM);
String to = request.getParameter(MAIL_TO);
String subject = request.getParameter(MAIL_SUBJECT);
String body = request.getParameter(MAIL_BODY);

try{
SmtpClient sc = new SmtpClient(host);
out.println("<br>");
sc.from(from);
sc.to(to);

PrintStream ps = new PrintStream(sc.startMessage());
ps.print("From:" + from);
ps.print("Subject:" + subject);
ps.print("To:" + to);
ps.print(body);
sc.closeServer();

out.println("<html>");
out.println("<body>");
out.println("send the mail successfully");
out.println("</body>");
out.println("</html>");
}catch(SmtpProtocolException e){
out.println("<html>");
out.println("<body>");
out.println(e);
out.println("</body>");
out.println("</html>");
}
}
/**Clean up resources*/
public void destroy() {
}
}

为什么邮件发送成功后,邮件的标题为“no subject",内容为空。
我用的邮件服务器是mail.sina.com.cn
...全文
686 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
assert_if 2002-04-12
  • 打赏
  • 举报
回复
呵呵,
爪蛙,我可是......,!联系我吧!

穷的冒汗!
java99666 2001-11-05
  • 打赏
  • 举报
回复
不好意思,请问:SmtpClient的基本方法都是什么?谢谢

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧