这是一个小的邮件脚本,从表单中获取信息,然后发送邮件。 它就像一个魅力只有一个问题是来自地址和主题不起作用。邮件没有来自地址且没有主题。
有人知道什么是错的?
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, Exception {
response.setContentType("text/html;charset=UTF-8");
//PrintWriter out = response.getWriter();
//SMTPserver en de mail naar waar de mensen hun mail versturen
String smtpServ = "uit.telenet.be";
String to = "ddd@gmail.com";
String from = request.getParameter("mail");
String subject = request.getParameter("subject") + "geschreven door: " + request.getParameter("naam");
String emailContent = request.getParameter("beschrijving");
response.setContentType("text/html");
try {
sendMessage(smtpServ, to, from, subject, emailContent);
} catch (Exception e) {
throw new ServletException(e.getMessage());
}
}
private void sendMessage(String smtpServer, String to, String from,
String subject, String emailContent) throws Exception {
try{
SmtpClient client = new SmtpClient(smtpServer);
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("Naar: " + to);
message.println("Onderwerp:" + subject);
message.println(emailContent);
message.println();
message.println("Het Burgerdemocratie Team");
message.println();
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR tijdens het verzenden van de mail:"+e);
}
}
答案 0 :(得分:0)
看来你使用sun类(根据名称SmtpClient,我们没有导入行)。
您应该使用 javax.mail 类。
您尝试获取的所有功能都可用:setSubject,setFrom等...
这是一个例子: Sending email using SMTP
答案 1 :(得分:0)
您忘了添加 sendMessage()方法:
message.println("To: " + to);
message.println("Subject: " + subject);