How send automatic reply on particular email id wh

2019-01-13 19:22发布

I have created a registration form in JSP with an input field for email address. When user submits the form, then the user must get an auto-reply on his/her email address. How can I achieve this?

3条回答
看我几分像从前
2楼-- · 2019-01-13 19:55

Your question is not very clear. Let me get the requirement right; you need the code to send an email to the user only on successful registration. correct?

In your servlet(which is invoked on submit action),

if(user input is valid){
  Step1: registerUser();
  Step2: send confirmationEmail();
} else {
  Step3: Exception case
}

Send email method would ideally send the request to an JMS(queue) to send the email to the desired user. Below is a snippet to send an email.

//Sample java code to send email

public void sendEmail(){

        try{

            Properties props = null;

                if (props == null) {
                        props = System.getProperties();
                }

                props.put("mail.smtp.host", "<server host name>");

                Session session = Session.getInstance(props, null);

                MimeMessage message = new MimeMessage(session);

                message.setFrom(new InternetAddress("<from email id>"));            

                message.addRecipients(Message.RecipientType.CC, "<Registered user email id>");          

                message.setSubject("<Subject of the email>");

                message.setContent("<Content of the email>", "text/plain");         

                Transport.send(message);    

                    logger.info("Sent Email :" + 
                        "From :" + message.getFrom() +
                        "To:" + message.getAllRecipients() +
                        "Subject:" + message.getSubject() );

            } catch(Exception ex){
                ex.printStackTrace();
            }       
        }
查看更多
smile是对你的礼貌
3楼-- · 2019-01-13 20:04

Auto-reply? Sorry, that term makes no sense in this particular context. Auto-reply is more a setting on a mailserver which should automatically send a reply back on incoming emails, for example "Thank you, your email is been received, your email will be answered within 24 hours." or something. You don't need this here.

You just want to programmatically send a mail. The mail should contain a link which should activate the account so that the user will be able to login. You see this indeed often on other websites. Here's how you can go about this:

  1. Setup/prepare a SMTP server. A SMTP server is a mail server. Like as that a HTTP server is a web server. You can use an existing one from your ISP or a public one like Gmail. You can even setup a completely own one. For example with Apache James. Whatever way you choose, you should end up knowing the hostname, portnumber, username and password of the SMTP server.

  2. Create a Mailer class which can take at least "from", "to", "subject" and "message" arguments and sends a mail using JavaMail. Connect and login the SMTP server by hostname, portnumber, username and password. Create a mail session and send the mail based on the given arguments. Create a dummy test class with a main() method which runs and tests the Mailer class. Once you get it to work, proceed with next steps.

  3. Create another database table user_activation with a PK key and FK user_id referring the PK id of an user table which you should already have. On the existing user table, add a boolean/bit field active which defaults to false/0.

  4. When user registration and insert in the DB succeeds, get the insert id from the user table, generate a long and unique key with java.util.UUID and insert them in user_activation table. Prepare a mail message with an activation link where the unique key is included as URL parameter or path and then send this message using the Mailer class you created.

  5. Create a Servlet which is mapped on an URL pattern matching the activation key links, e.g. /activate/* and extracts the activation key from the URL. Select the associated user from the database and if any exist, then set its active field to true/1 and remove the key from user_activation table.

  6. On login, only select the user when active=true or 1.

查看更多
Emotional °昔
4楼-- · 2019-01-13 20:20

What about adding user's email address to Bcc field (Blind Carbon Copy)?

查看更多
登录 后发表回答