Мне нужна кодировка для электронной почты с вложением с использованием сервлета.Пожалуйста, помогите мне.Я новичок в сервлетах и JSP.Мне нужно и кодирование JSP, и сервлета.
Вот что я пробовал до сих пор -
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sending email</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<center>
<form action="EmailServlet" method="post" name="ss">
<table>
<tr>
<td>To</td>
<td><input type="text" name="to"></td>
</tr>
<tr>
<td>cc</td>
<td><input type="text" name="cc"></td>
</tr>
<tr>
<td>bcc</td>
<td><input type="text" name="bcc"></td>
</tr>
<tr>
<td>From</td>
<td><input type="text" name="from"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="subject"></td>
</tr>
<tr>
<td>attach</td>
<td><input type="file" name="file1"></td>
</tr>
<tr>
<td>attach2</td>
<td><input type="file" name="file2"></td>
</tr>
<tr>
<td>Message</td>
<td><textarea cols="25" rows="8" name="message"></textarea></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="password"></td>
</tr>
</table>
<br>
<input type="submit" value="submit" onclick=" return soora();">
</form>
</center>
</body>
</html>
МОЯ страница сервлета:
import java.io.*;
import java.net.*;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmailServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
final String err = "/error.jsp";
final String succ = "/success.jsp";
String from = request.getParameter("from");
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
String login = request.getParameter("login");
String password = request.getParameter("password");
try {
Properties props = new Properties();
props.setProperty("mail.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator(login, password);
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
Transport.send(msg);
} catch (AuthenticationFailedException ex) {
request.setAttribute("ErrorMessage", "Authentication failed");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
} catch (AddressException ex) {
request.setAttribute("ErrorMessage", "Wrong email address");
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
} catch (MessagingException ex) {
request.setAttribute("ErrorMessage", ex.getMessage());
RequestDispatcher dispatcher = request.getRequestDispatcher(err);
dispatcher.forward(request, response);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
dispatcher.forward(request, response);
}
private class SMTPAuthenticator extends Authenticator {
private PasswordAuthentication authentication;
public SMTPAuthenticator(String login, String password) {
authentication = new PasswordAuthentication(login, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
Я получил ошибку нижекак это:
java.lang.IllegalStateException: Cannot forward after response has been committed
EmailServlet.processRequest(EmailServlet.java:71)
EmailServlet.doPost(EmailServlet.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)