2016-04-25 130 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 
相關問題