1
+ package com .alaris .Analyzer .ui ;
2
+
3
+ import java .util .Properties ;
4
+
5
+ import javax .mail .Message ;
6
+ import javax .mail .MessagingException ;
7
+ import javax .mail .PasswordAuthentication ;
8
+ import javax .mail .Session ;
9
+ import javax .mail .Transport ;
10
+ import javax .mail .internet .InternetAddress ;
11
+ import javax .mail .internet .MimeMessage ;
12
+
13
+ public class SendMail {
14
+
15
+ public static void main (String [] args ) {
16
+
17
+ // Recipient's email ID needs to be mentioned.
18
+
19
+
20
+ // Sender's email ID needs to be mentioned
21
+ String from =
"[email protected] " ;
22
+
23
+ // Assuming you are sending email from through gmail smtp
24
+ String host = "smtp.gmail.com" ;
25
+
26
+ // Get system properties
27
+ Properties properties = System .getProperties ();
28
+
29
+ // Setup mail server
30
+ properties .put ("mail.smtp.host" , host );
31
+ properties .put ("mail.smtp.port" , "465" );
32
+ properties .put ("mail.smtp.ssl.enable" , "true" );
33
+ properties .put ("mail.smtp.auth" , "true" );
34
+
35
+ // Get the Session object.// and pass username and password
36
+ Session session = Session .getInstance (properties , new javax .mail .Authenticator () {
37
+
38
+ protected PasswordAuthentication getPasswordAuthentication () {
39
+
40
+ return new PasswordAuthentication (
"[email protected] " ,
"appsPassword" );
41
+
42
+ }
43
+
44
+ });
45
+
46
+ // Used to debug SMTP issues
47
+ session .setDebug (true );
48
+
49
+ try {
50
+ // Create a default MimeMessage object.
51
+ MimeMessage message = new MimeMessage (session );
52
+
53
+ // Set From: header field of the header.
54
+ message .setFrom (new InternetAddress (from ));
55
+
56
+ // Set To: header field of the header.
57
+ message .addRecipient (Message .RecipientType .TO , new InternetAddress (to ));
58
+
59
+ // Set Subject: header field
60
+ message .setSubject ("This is the Subject Line!" );
61
+
62
+ // Now set the actual message
63
+ message .setText ("This is actual message" );
64
+
65
+ System .out .println ("sending..." );
66
+ // Send message
67
+ Transport .send (message );
68
+ System .out .println ("Sent message successfully...." );
69
+ } catch (MessagingException mex ) {
70
+ mex .printStackTrace ();
71
+ }
72
+
73
+ }
74
+
75
+ }
0 commit comments