Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MailServlet extends HttpServlet
- {
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
- ServletException, IOException
- {
- try {
- sendMail("smtpHost", "user", "pass", "from", "to", "subject","text");
- } catch (Exception e) {
- }
- }
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException
- {}
- public static void sendMail(String smtpHost,String username,String password,String senderAddress,String recipientsAddress,String subject,String text ) throws Exception
- {
- MailAuthenticator auth = new MailAuthenticator(username, password);
- Properties properties = new Properties();
- properties.put("mail.smtp.host", smtpHost);
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.socketFactory.port", "465");
- properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- Session session = Session.getDefaultInstance(properties, auth);
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(senderAddress));
- msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientsAddress, false));
- msg.setSubject(subject);
- msg.setText(text);
- msg.setHeader("Recall", "Recall");
- msg.setSentDate(new Date( ));
- Transport.send(msg);
- }
- }
- class MailAuthenticator extends Authenticator
- {
- private final String user;
- private final String password;
- public MailAuthenticator(String user, String password)
- {
- this.user = user;
- this.password = password;
- }
- protected PasswordAuthentication getPasswordAuthentication()
- {
- return new PasswordAuthentication(this.user, this.password);
- }
- }
Add Comment
Please, Sign In to add comment