<html>
<head>
<title>Send Email</title>
</head>
<body>
<h1 align="center">Email Sending Client</h1>
</tr></td>
<table width=30% align="center">
<form id=myform method="get" action="sendemail" onSubmit="return validate();">
Your html form code
</form>
</html>
sendemail.java внутри пакета com.zetcode
package com.zetcode;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.internet.*;
public class sendemail 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");
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("username","password");
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("name <"+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);
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
сделать две другие страницы error.jsp и success.jsp, чтобы перенаправить их на конкретную страницу, чтобы узнать, отправлено письмо или нет ...
этот код работает для меня
Я надеюсь .. теперь ты можешь сделать это ..