Subversion Repository Public Repository

FP_REPORT_SEND_MAIL

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.nru.test;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class mailer {
	final String SSL_FACTORY 		= "javax.net.ssl.SSLSocketFactory";
	String smtp_host_name 			= "smtp.gmail.com";
	String smtp_mail_port 			= "465";
	String smtp_store_protocol 		= "pop3";
	String smtp_transport_protocol 	= "smtp";
	String mail_account_username 	= "";
	String mail_account_password 	= "";
	String mail_from 				= "";
	String mail_to 					= "";
	String mail_subject				= "";
	String mail_message				= "";
	
	FileReader reader;
	String mysql_driver_string="";
	String mysql_connection_string="";
	String mysql_username="";
	String mysql_password="";
	String query ="";
	
	public void readConfig(){
		
	}
	
	public mailer(Vector data) {
		try {
			reader = new FileReader("config.properties");
			Properties properties=new Properties();
			properties.load(reader);
			final String mail_account_username=properties.getProperty("mail_account_username");
			final String mail_account_password=properties.getProperty("mail_account_password");
			String mail_from=properties.getProperty("mail_from");
			String mail_to=properties.getProperty("mail_to");
			String mail_subject=properties.getProperty("mail_subject");
			String mail_message=properties.getProperty("mail_message");
			
			this.mail_account_username 	= mail_account_username;
			this.mail_account_password 	= mail_account_password;
			this.mail_from 				= mail_from;
			this.mail_to 				= mail_to;
			this.mail_subject 			= mail_subject;
			this.mail_message 			= mail_message;
			
			 Properties props = System.getProperties();
		     props.setProperty("mail.smtp.host", smtp_host_name);
		     props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		     props.setProperty("mail.smtp.socketFactory.fallback", "false");
		     props.setProperty("mail.smtp.port", smtp_mail_port);
		     props.setProperty("mail.smtp.socketFactory.port", smtp_mail_port);
		     props.put("mail.smtp.auth", "true");
		     //props.put("mail.debug", "true");
		     props.put("mail.store.protocol", smtp_store_protocol);
		     props.put("mail.transport.protocol", smtp_transport_protocol);

		     Session session = Session.getInstance(props, new Authenticator() {
		        @Override
		        protected PasswordAuthentication getPasswordAuthentication() {
		            return new PasswordAuthentication(mail_account_username, mail_account_password);
		        }
		    });

		    String mail_message_body = getMessageBody(data);
		    
		    MimeMessage msg = new MimeMessage(session);
	        msg.setFrom();
	        msg.setRecipients(Message.RecipientType.TO,mail_to);
	        msg.setSubject(mail_subject);
	        msg.setSentDate(new Date());
	        //msg.setText(mail_message);				// send mail as plain text
	        
	        
	        
	        MimeMultipart multipart = new MimeMultipart("related");
	         // first part (the html)
	         BodyPart messageBodyPart = new MimeBodyPart();
	        // String htmlText = "<H1>Hey man find the below image</H1><img src=\"cid:image\">";
	         messageBodyPart.setContent(mail_message_body, "text/html");
	         // add it
	         multipart.addBodyPart(messageBodyPart);
	         // second part (the image)
	         messageBodyPart = new MimeBodyPart();
	         DataSource fds = new FileDataSource(
	            "ext/image_new1.jpg");
	        /* DataSource fds1 = new FileDataSource(
	 	            "ext/my.jpg");*/
	         messageBodyPart.setDataHandler(new DataHandler(fds));
	         messageBodyPart.setHeader("Content-ID", "<image>");
	        /* messageBodyPart.setDataHandler(new DataHandler(fds1));
	         messageBodyPart.setHeader("Content-ID", "<image>");*/
	         // add image to the multipart
	         multipart.addBodyPart(messageBodyPart);
	         // put everything together
	         msg.setContent(multipart); 
	         Transport.send(msg);

	        
	       // msg.setContent(mail_message_body, "text/html");	// send mail as html content type. you can use br, underline, bold etc
	       // Transport.send(msg);
	        System.out.println("Message sent successfuly !!!");
		} catch (Exception e) {System.out.println("send failed, exception: " + e);}
	}
	
	public String getMessageBody(Vector data) throws ParseException{
		/*DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");*/
        
		Date today = new Date();
		String today_str=today.getDate()+"-"+(1+today.getMonth())+"-"+(1900+today.getYear())+"";
		System.out.println("today_str"+today_str);

		/*Date todayWithZeroTime = formatter.parse(formatter.format(today));*/
		
		
		/*Date date=new Date();*/
		String mail_message_body = "<table border='1px' bgcolor=E3E0F4>";
		
		/*mail_message_body+="<style type='text/css'>";
		mail_message_body+="@-webkit-keyframes blinker {";
		mail_message_body+="from {opacity: 1.0;}";
		mail_message_body+="to {opacity: 0.0;}";
		mail_message_body+="}";
		mail_message_body+=".blink {";
		mail_message_body+="text-decoration: blink";
		mail_message_body+="-webkit-animation-name: blinker";
		mail_message_body+="-webkit-animation-duration: 0.6s";
		mail_message_body+="-webkit-animation-iteration-count:infinite";
		mail_message_body+="-webkit-animation-timing-function:ease-in-out";
		mail_message_body+="-webkit-animation-direction: alternate";
		mail_message_body+="}";
		mail_message_body+="</style>";*/
		/*mail_message_body+="<img src=\"http://www.rgagnon.com/images/jht.gif\">";*/
		//mail_message_body+="<tr><td colspan='4'><center><img src=\"cid:image\"></center></td><tr>";
	//	mail_message_body += "<tr><td colspan='4'> <center><img src=\"cid:image\"><h3> *** "+mail_message+" *** </h3><br>"+"Date:"+(today_str)+"</center></td></tr>";
		mail_message_body += "<tr><td colspan='1'><img src=\"cid:image\"></td><td colspan='3'><center> <br><h2><i>DAILY <br>ATTENDANCE</i></h2>"+"<marquee><h3><i>DATE:"+(today_str)+"</i></h3></marquee></center></td></tr>";
		mail_message_body += "<tr bgcolor=E3E0F4><th><center>Name</center></th> <th><center>Checkin</center></th> <th><center>Checkout</center></th><th><center>Duration</center></th></tr>";
		System.out.println("data "+data+" data size"+data.size());
		
		
		DateFormat dateFormat = new SimpleDateFormat("hh:mma");
		Date date1;
		Date date2;
		
		for (int i = 0; i < data.size(); i++) {
			Vector<String> records = (Vector<String>)data.elementAt(i);
			String line = "<tr>";
		//	System.out.println("rr "+records+" size"+records.size());
			for(int j =0; j < records.size(); j++){
				
				date1 = dateFormat.parse("09:10AM");
				date2 = dateFormat.parse(records.elementAt(1));
				if(date2.before(date1)){
					line += "<td style='padding:10px;'><center>"+records.elementAt(j)+"</td>";
					
				}else{
					line += "<td  bgcolor=F3776F style='padding:10px;'><center>"+records.elementAt(j)+"</td>";
					System.out.println("records" +records.elementAt(j));
				}
				
				//System.out.println("d1@@@"+date);
				//if(records.elementAt(j)=="09:10AM")
				
			}
			line += "</tr>";
			//mail_message_body += html_singleline+java_singleline;
			mail_message_body += line;
		}
		 
		mail_message_body += "<tr><td colspan='4'><center><i><h5>Please note this is an autogenerated mail.<br> Please <u>DO NOT REPLY</u> to this mail.</h5></i></center></td></tr>";
		mail_message_body += "</table>";

		return mail_message_body;
	}

	public static void main(String[] args){
		// Testing
		/*Vector vector = new Vector();
		Vector temp = new Vector();
		temp.add("Test1");
		temp.add("2018-08-16 12:45:56");
		temp.add("2018-08-16 19:45:56");
		vector.add(temp);
		
		temp = new Vector();
		temp.add("Test2");
		temp.add("2018-08-15 12:45:56");
		temp.add("2018-08-15 19:45:56");
		vector.add(temp);
		
		mailer mail = new mailer(vector);*/
	}
}



Commits for FP_REPORT_SEND_MAIL/src/com/nru/test/mailer.java

Diff revisions: vs.
Revision Author Commited Message
1 lingaraj picture lingaraj Sat 24 Nov, 2018 10:13:09 +0000