我正在開發一個基於jsp的後端功能的Web應用程序。到目前爲止,任何用戶都可以在沒有任何程序步驟的情況下進行註冊和登錄。爲了使網絡應用程序更真實,我想要一個功能,用戶註冊並接收電子郵件通知,爲他們提供一個歡迎信息和驗證其帳戶的鏈接。什麼是最好的僞代碼,以及如何將我的註冊表單與發送通知電子郵件給用戶的電子郵件服務器相集成?JSP-Web應用程序電子郵件通知註冊和帳戶驗證
0
A
回答
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
相關問題
- 1. 用戶註冊電子郵件驗證
- 2. 用戶註冊,電子郵件驗證和Ruby驗證
- 3. ASP.Net電子郵件和帳戶驗證
- 4. 用電子郵件註冊用戶確認和驗證碼?
- 5. 註冊用戶:驗證碼和確認電子郵件?
- 6. Android - 通過電子郵件地址註冊和驗證
- 7. 用戶註冊的電子郵件密鑰驗證
- 8. TeamCity:向非註冊用戶發送電子郵件通知
- 9. 驗證已登記的用戶(通過電子郵件)在facebook註冊插件
- 10. 驗證電子郵件與帳戶谷歌,帳戶Facebook的
- 11. 流星帳戶電子郵箱驗證
- 12. 通過電子郵件帳戶驗證 - 利弊
- 13. C2DM註冊郵件帳戶
- 14. 如何通知android應用程序該電子郵件驗證鏈接點擊?
- 15. 如何在註冊用戶時驗證Drupal 7中的用戶電子郵件
- 16. 註冊應用程序以閱讀電子郵件附件
- 17. 流星用戶帳戶電子郵件驗證
- 18. Laravel 5.1如何在註冊後發送電子郵件驗證電子郵件?
- 19. 通過電子郵件和PHP進行電子郵件驗證?
- 20. 用電子郵件驗證註冊,只有30%被激活?
- 21. auth0 - 電子郵件驗證 - 用戶帳戶不存在或驗證碼無效
- 22. 註冊Actuate iServer Express電子郵件帳戶
- 23. 在symfony2中通過電子郵件鏈接驗證用戶帳戶
- 24. 在Drupal帳戶註冊期間自動生成電子郵件帳戶?
- 25. 電子郵件驗證程序
- 26. 電子郵件SMTP驗證程序
- 27. 試圖使用Gmail帳戶驗證撰寫電子郵件
- 28. 註冊Jquery郵件驗證
- 29. Asp.NET MVC 5通過電子郵件和密碼驗證用戶
- 30. Firebase通過電子郵件和Google帳戶進行身份驗證