2016-04-25 129 views
0

我正在开发一个基于jsp的后端功能的Web应用程序。到目前为止,任何用户都可以在没有任何程序步骤的情况下进行注册和登录。为了使网络应用程序更真实,我想要一个功能,用户注册并接收电子邮件通知,为他们提供一个欢迎信息和验证其帐户的链接。什么是最好的伪代码,以及如何将我的注册表单与发送通知电子邮件给用户的电子邮件服务器相集成?JSP-Web应用程序电子邮件通知注册和帐户验证

回答

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)以及服务器的用户名和密码(你应该从你的网站托管服务提供商此信息)。最后一个参数是激活码。

本示例还使用HTML编码消息。

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("[email protected]"); 
email.setSubject("TestMail"); 
email.setMsg("This is a test mail ... :-)"); 
email.addTo("[email protected]"); 
email.send(); 


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