- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailSend.java
More file actions
Latest commit
61 lines (53 loc) · 2.06 KB
/
Copy pathEmailSend.java
File metadata and controls
61 lines (53 loc) · 2.06 KB
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
/*
* https://www.google.com/settings/security/lesssecureapps
*/
packageSendEmail;
importjava.util.*;
importjavax.mail.*;
importjavax.mail.internet.*;
importjavax.mail.internet.MimeMessage;
publicclassEmailSend {
publicstaticvoidmain(Stringargs[]){
try{
SendMail(to,subject,msg);
}catch(Exceptionex)
{
System.out.println(ex);
}
}
publicstaticvoidSendMail(StringtoMail,StringsubjectMail,Stringcontext)
{
Stringhost ="smtp.gmail.com" ;
Stringuser = "your email";
Stringpass = "your password";
Stringto = toMail;
Stringfrom = "your email";
Stringsubject = subjectMail;
StringmessageText = context;
booleansessionDebug = false;
Propertiesprops = 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(newcom.sun.net.ssl.internal.ssl.Provider());
SessionmailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
try {
Messagemsg = newMimeMessage(mailSession);
msg.setFrom(newInternetAddress(from));
InternetAddress[] address = {newInternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject); msg.setSentDate(newDate());
msg.setText(messageText);
Transporttransport=mailSession.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("message send successfully");
} catch (MessagingExceptione) {
thrownewRuntimeException(e);
}
}
}