JSP-web app关于注册和帐户验证的电子邮件通知

时间:2016-04-25 13:10:57

标签: jsp javamail email-verification

我正在开发一个带有基于jsp的后端的Web应用程序来实现功能。到目前为止,任何用户都可以在运行时注册和登录,而无需任何程序步骤。为了使网络应用程序更真实,我希望有一个功能,用户可以通过该功能注册并接收电子邮件通知,向他们发送欢迎消息和验证其帐户的链接。什么是最好的伪代码,以及如何将我的注册表单与向用户发送通知电子邮件的电子邮件服务器集成?

2 个答案:

答案 0 :(得分:0)

您可以使用javax.mail API发送电子邮件通知。以下是使用激活密钥的完整功能代码。

public static void sendMail(String toAddress,String user,String psw, String key){

    final String msg = "<html><body><h3>** THIS IS AN AUTOMATED MESSAGE - PLEASE, DO NOT REPLY **</h3>This e-mail has been sent to you automatically as part of the registration process.<br> "
            +"To activate your account, click the activation link below.<br><br>"+key+"</body></html>"; 
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", "mail.dom.info");
    properties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    properties.setProperty("mail.smtp.socketFactory.fallback", "false");
    properties.setProperty("mail.smtps.auth", "true");      
    properties.put("mail.smtps.quitwait", "false");

    Session session = Session.getDefaultInstance(properties);
    try{
         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(user));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));

         message.setSubject("Your account activation");
        // message.setText(msg, "utf-8");
         message.setContent(msg, "text/html");  
         message.setSentDate(new Date());
         SMTPTransport transport = (SMTPTransport)session.getTransport("smtps");

         transport.connect("mail.dom.info", user, psw);
         transport.sendMessage(message, message.getAllRecipients());      
         transport.close();
         //System.out.println("Message sent....");
      }catch (MessagingException e) {
         e.printStackTrace();
      }
}

请注意,您必须指定SMTP服务器(在本例中为mail.dom.info)以及服务器的用户名和密码(您应该从您的Web托管服务提供商处获取此信息)。最后一个参数是激活密钥。

此示例还对邮件使用HTML编码。

答案 1 :(得分:0)

Apache Commons Email API is also available for sending E-Mail.

If you have gmail account than these are few gmail account SMTP configuration using this you can send E-mail.

Gmail SMTP server name: smtp.gmail.com


Gmail SMTP username: your Gmail address


Gmail SMTP password: your password


Gmail SMTP port: 465

using this configuration you can send email using your gmail account. For other service provider you have to find its SMTP settings and provide in your api configuration.

Using apache commons api its quite easy like this :

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();


You can find more example from this : [1]:https://commons.apache.org/proper/commons-email/userguide.html