-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendEmail.java
71 lines (58 loc) · 2.02 KB
/
SendEmail.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Random;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail extends HttpServlet
{
public void service(HttpServletRequest req , HttpServletResponse res)throws IOException , ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String s="1234567890";
Random random=new Random();
char[] otp=new char[6];
for(int i=0;i<6;i++){
otp[i]=s.charAt(random.nextInt(s.length()));
}
String strArray[]=new String[otp.length];
for(int i=0;i<otp.length;i++){
strArray[i]=String.valueOf(otp[i]);
}
String s1=Arrays.toString(strArray);
String res1="";
for(String num:strArray){
res1+=num;
}
String to=(String)req.getAttribute("email");
String from="[email protected]";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
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("Sbi otp");
// Now set the actual message
message.setText("your one time password is:" +res1);
// Send message
Transport.send(message);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
req.setAttribute("otp",res1);
RequestDispatcher disp=req.getRequestDispatcher("Verify");
disp.include(req,res);
}
}