2016-04-25 10 views
0

Ich entwickle eine Web-Anwendung mit einem jsp-basierten Backend für die Funktionalitäten. Bislang kann sich jeder Benutzer ohne Zwischenschritte registrieren und anmelden. Um die Web-App authentischer zu machen, möchte ich eine Funktion haben, mit der sich ein Benutzer anmeldet und eine E-Mail-Benachrichtigung erhält, die ihm eine Willkommensnachricht und einen Link zur Bestätigung seines Kontos gibt. Was wäre der beste Pseudocode für die Zusammenarbeit und wie würde ich mein Anmeldeformular in den E-Mail-Server integrieren, der die Benachrichtigungs-E-Mail an den Benutzer sendet?JSP-Web App E-Mail-Benachrichtigung bei Anmeldung und Kontoverifizierung

Antwort

0

Sie können die javax.mail-API zum Senden von E-Mail-Benachrichtigungen verwenden. Der folgende Code ist voll funktionsfähig und verwendet einen Aktivierungsschlüssel.

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(); 
     } 
} 

Beachten Sie, dass Sie Ihren SMTP-Server (in diesem Fall mail.dom.info) sowie den Benutzernamen und das Kennwort für den Server angeben müssen (Sie sollten diese Informationen von Ihrem Web-Hosting-Provider erhalten). Der letzte Parameter ist der Aktivierungsschlüssel.

In diesem Beispiel wird auch HTML-Codierung für die Nachricht verwendet.

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 
Verwandte Themen