-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEmailSend.java
55 lines (42 loc) · 1.84 KB
/
EmailSend.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
package Mailer;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;
public class EmailSend {
public void sendMail(String email ,String password){
try{
String host ="smtp.gmail.com" ;
String user = "nilay250@gmail.com";
String pass = "kapaley.nilay0114cs161062";
String to = email;
String from = "nilay250@gmail.com";
String subject = "Password Reset";
String messageText = "Hi, "+email+". your password is "+password;
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());//For Security
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject); msg.setSentDate(new Date());
msg.setText(messageText);
Transport transport=mailSession.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("message send successfully");
}catch(Exception ex)
{
System.out.println(ex);
}
}
}