我已经在JSP中创建了一个注册表单,其中输入了电子邮件地址。用户提交表单时,用户必须在他/她的电子邮件地址上获得自动答复。我该如何实现?
您的问题不是很清楚。让我弄清需求吧;您只需要成功注册即可将代码发送给用户的代码。正确?
在您的servlet(在Submit操作中调用)中,
if(user input is valid){ Step1: registerUser(); Step2: send confirmationEmail(); } else { Step3: Exception case }
发送电子邮件方法理想情况下会将请求发送到JMS(队列),以将电子邮件发送给所需的用户。以下是发送电子邮件的代码段。
//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(); } }