java 如何通过在jsp中发送邮件来进行电子邮件验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14743055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do email verification by sending the mail in jsp
提问by rushang
I am having the registration form.After submitting it goes to another JSP page where it gets the data of that form and stores it in my database.Now i want to send the email to the user id which he has given for the confirmation.I don't have any idea about it. please get me code for it. This is my registration form action and submit code.
我有注册表单。提交后,它会转到另一个 JSP 页面,在那里它获取该表单的数据并将其存储在我的数据库中。现在我想将电子邮件发送给他为确认提供的用户 ID。不知道。请给我代码。这是我的注册表单操作和提交代码。
<form action="signupdata.jsp" method="POST">
<input type="submit" onclick="" value="submit">
This is on signupdata.jsp to save the data in my database.
这是在 signupdata.jsp 上,用于将数据保存在我的数据库中。
int i=st.executeUpdate("insert into userdetails(firstname,lastname,email,password,cpassword,category) values('"+fname+"','"+lname+"','"+emal+"','"+pwd+"','"+cpwd+"','"+ctgry+"')");
Now I want to send the email of confirmation to the user's email ID like most of websites provides.
现在我想像大多数网站提供的那样将确认电子邮件发送到用户的电子邮件 ID。
采纳答案by Rais Alam
Write below code in firtPage.jsp
在firtPage.jsp 中写入以下代码
<form action="signupdata.jsp" method="POST">
Please enter email : <input type="text" name="email">
<input type="submit" onclick="" value="submit">
</form>
Write below code in signupdata.jsp
在signupdata.jsp 中写入以下代码
// Recipient's email ID needs to be mentioned.
String to = request.getParameter("email");
// Sender's email ID needs to be mentioned
String from = "SENDER_EMAIL";
// Assuming you are sending email from localhost
String host = "YOUR_EMAIL_HOST";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("SUVJECT_LINE");
// Now set the actual message
message.setText("YOUR MESSAGE GOES HERE");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
回答by Kshitiz Sharma
The exact code for the problem depends upon your project and can be too large to post here. I would recommend that instead of looking for code snippets you should ask for general solutions/libraries that fit your problem domain and then google for code samples based on that.
问题的确切代码取决于您的项目,并且可能太大而无法在此处发布。我建议您不要寻找代码片段,而是询问适合您的问题域的通用解决方案/库,然后在谷歌上搜索基于此的代码示例。
In this case you can use Java Mail API
to send mails. Just google for Java Mail code samples.
在这种情况下,您可以使用Java Mail API
发送邮件。只是谷歌的 Java 邮件代码示例。
If you are using Spring framework JavaMailSenderImpl
provides a convenient way to send mails.
如果您使用的是 Spring 框架,则JavaMailSenderImpl
提供了一种发送邮件的便捷方式。
<bean id="mailSender" class="com.kshitiz.MailUtil"
init-method="init" destroy-method="destroy">
<constructor-arg index="0">
<bean id="mailSenderMain" class="org.springframework.mail.javamail.JavaMailSenderImpl"
p:host="${host}" p:port="${port}"
p:username="${username}" p:password="${password}">
<property name="javaMailProperties">
<props>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
</constructor-arg>
</bean>
Then in code -
然后在代码中 -
public class MailUtil {
private MailSender mailSender;
public MailUtil(MailSender mailSender)
{
this.mailSender=mailSender;
}
public void sendResetPasswordMail(String email, String password) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("[email protected]");
message.setTo(email);
message.setSubject("Your new password!");
message.setText("Here is your new account login password - " + password);
mailSender.send(message);
}
}
For verification you could add a field auth_token
to your user_accounts table. This auth_token is a UUID
generated at the time of registration. Then create a servlet that takes a parameter and verifies it against the database. Send the link to this servlet in the mail. If verification is successful erase the auth_token
field marking the user as authenticated.
为了验证,您可以auth_token
在 user_accounts 表中添加一个字段。这个 auth_token 是UUID
在注册时生成的。然后创建一个 servlet,它接受一个参数并根据数据库验证它。在邮件中发送此 servlet 的链接。如果验证成功,请擦除auth_token
将用户标记为已通过身份验证的字段。