Subversion Repositories SmartDukaan

Rev

Rev 12103 | Rev 31028 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12103 Rev 20152
Line 35... Line 35...
35
	public GmailUtils(){
35
	public GmailUtils(){
36
	}
36
	}
37
 
37
 
38
	public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename) throws MessagingException {
38
	public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, String filename) throws MessagingException {
39
		try {
39
		try {
40
			Message msg = prepareMessage(recipients, subject, from, password);
40
			Message msg = prepareMessage(recipients,null, subject, from, password);
41
			//Create the multi-part object to hold the text and attachment parts
41
			//Create the multi-part object to hold the text and attachment parts
42
			Multipart multipart = new MimeMultipart();
42
			Multipart multipart = new MimeMultipart();
43
			// create the message part 
43
			// create the message part 
44
			MimeBodyPart messageBodyPart = new MimeBodyPart();
44
			MimeBodyPart messageBodyPart = new MimeBodyPart();
45
			//Part 1: Text message
45
			//Part 1: Text message
Line 104... Line 104...
104
 
104
 
105
				htmlContent.append(line).append("\n");
105
				htmlContent.append(line).append("\n");
106
 
106
 
107
			}
107
			}
108
 
108
 
109
			Message msg = prepareMessage(recipients, subject, from, password);
109
			Message msg = prepareMessage(recipients,null, subject, from, password);
110
			//Create the multi-part object to hold the text and attachment parts
110
			//Create the multi-part object to hold the text and attachment parts
111
			Multipart multipart = new MimeMultipart();
111
			Multipart multipart = new MimeMultipart();
112
 
112
 
113
			MimeBodyPart htmlPart = new MimeBodyPart();
113
			MimeBodyPart htmlPart = new MimeBodyPart();
114
 
114
 
Line 156... Line 156...
156
	}
156
	}
157
 
157
 
158
	private String extractDomainFromEmailAddress(String recipient) {
158
	private String extractDomainFromEmailAddress(String recipient) {
159
		return recipient.split("@")[1];
159
		return recipient.split("@")[1];
160
	}
160
	}
-
 
161
	
-
 
162
	
161
 
163
 
162
	public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, List<File> files) throws MessagingException {
164
	public void sendSSLMessage(String recipients[], String subject, String message, final String from, final String password, List<File> files) throws MessagingException {
163
		try {
165
		try {
164
			Message msg = prepareMessage(recipients, subject, from, password);
166
			Message msg = prepareMessage(recipients,null, subject, from, password);
165
 
167
 
166
			//Create the multi-part object to hold the text and attachment parts
168
			//Create the multi-part object to hold the text and attachment parts
167
			Multipart multipart = new MimeMultipart();
169
			Multipart multipart = new MimeMultipart();
168
 
170
 
169
			// create the message part 
171
			// create the message part 
Line 197... Line 199...
197
 
199
 
198
			throw new MessagingException(e.getMessage(), e);
200
			throw new MessagingException(e.getMessage(), e);
199
		}
201
		}
200
	}
202
	}
201
 
203
 
-
 
204
	
-
 
205
	public void sendSSLMessage(String recipients[],String cc[], String subject, String message, String from, String password, List<File> files) throws MessagingException {
-
 
206
		try {
-
 
207
			Message msg = prepareMessage(recipients,cc, subject, from, password);
-
 
208
 
-
 
209
			//Create the multi-part object to hold the text and attachment parts
-
 
210
			Multipart multipart = new MimeMultipart();
-
 
211
 
-
 
212
			// create the message part 
-
 
213
			MimeBodyPart messageBodyPart = new MimeBodyPart();
-
 
214
			//Part 1: Text message
-
 
215
			messageBodyPart.setText(message);
-
 
216
			multipart.addBodyPart(messageBodyPart);
-
 
217
 
-
 
218
			//Part 2: Attachment
-
 
219
			for (File file : files) {
-
 
220
				DataSource source = new FileDataSource(file);
-
 
221
				String[] names = file.getAbsolutePath().split("/");
-
 
222
				messageBodyPart = new MimeBodyPart();
-
 
223
				messageBodyPart.setDataHandler(new DataHandler(source));
-
 
224
				messageBodyPart.setFileName(names[names.length - 1]);
-
 
225
				multipart.addBodyPart(messageBodyPart);
-
 
226
			}
-
 
227
 
-
 
228
			// Put parts in message
-
 
229
			msg.setContent(multipart);
-
 
230
			/*if(cc!=null){
-
 
231
				//Set the cc
-
 
232
				InternetAddress[] addressCC = new InternetAddress[cc.length];
-
 
233
				for (int i = 0; i < cc.length; i++) {
-
 
234
					addressCC[i] = new InternetAddress(cc[i]);
-
 
235
				}
-
 
236
			Transport.send(msg, addressCC);
-
 
237
			}else{*/
-
 
238
				Transport.send(msg);
-
 
239
		//	}
-
 
240
		} catch (Exception e) {
-
 
241
			// If there is any error with normal mail sending, try diving receipients across domains
-
 
242
			e.printStackTrace();
-
 
243
			if (hasMultipleDomains(recipients)) {
-
 
244
				for (List<String> subReceipients : getReceipientsPerDomain(recipients).values()) {
-
 
245
					sendSSLMessage(subReceipients.toArray(new String[0]),cc, subject, message, from, password, files);
-
 
246
				}
-
 
247
 
-
 
248
				return;
-
 
249
			}
-
 
250
 
-
 
251
			throw new MessagingException(e.getMessage(), e);
-
 
252
		}
-
 
253
	}
-
 
254
 
202
	@SuppressWarnings("restriction")
255
	@SuppressWarnings("restriction")
203
	private Message prepareMessage(String[] recipients, String subject,
256
	private Message prepareMessage(String[] recipients,String[] cc, String subject,
204
			final String from, final String password) throws AddressException,
257
			final String from, final String password) throws AddressException,
205
			MessagingException {
258
			MessagingException {
206
		boolean debug = true;
259
		boolean debug = true;
207
 
260
 
208
		Properties props = new Properties();
261
		Properties props = new Properties();
Line 235... Line 288...
235
		InternetAddress[] addressTo = new InternetAddress[recipients.length];
288
		InternetAddress[] addressTo = new InternetAddress[recipients.length];
236
		for (int i = 0; i < recipients.length; i++) {
289
		for (int i = 0; i < recipients.length; i++) {
237
			addressTo[i] = new InternetAddress(recipients[i]);
290
			addressTo[i] = new InternetAddress(recipients[i]);
238
		}
291
		}
239
		msg.setRecipients(Message.RecipientType.TO, addressTo);
292
		msg.setRecipients(Message.RecipientType.TO, addressTo);
-
 
293
		
-
 
294
		if(cc!=null){
-
 
295
			//Set the cc
-
 
296
			InternetAddress[] addressCC = new InternetAddress[cc.length];
-
 
297
			for (int i = 0; i < cc.length; i++) {
-
 
298
				addressCC[i] = new InternetAddress(cc[i]);
-
 
299
			}
-
 
300
			msg.setRecipients(Message.RecipientType.CC, addressCC);
-
 
301
		}
-
 
302
		
240
 
303
 
241
		//Setting the Subject
304
		//Setting the Subject
242
		msg.setSubject(subject);
305
		msg.setSubject(subject);
243
		return msg;
306
		return msg;
244
	}
307
	}
245
	
308
	
246
	public void sendSSLMessage(String recipients[], String subject,final String from, final String password,String htmlContent) throws MessagingException {
309
	public void sendSSLMessage(String recipients[], String subject,final String from, final String password,String htmlContent) throws MessagingException {
247
		try {
310
		try {
248
			Message msg = prepareMessage(recipients, subject, from, password);
311
			Message msg = prepareMessage(recipients,null, subject, from, password);
249
			//Create the multi-part object to hold the text and attachment parts
312
			//Create the multi-part object to hold the text and attachment parts
250
			Multipart multipart = new MimeMultipart();
313
			Multipart multipart = new MimeMultipart();
251
			MimeBodyPart htmlPart = new MimeBodyPart();
314
			MimeBodyPart htmlPart = new MimeBodyPart();
252
			htmlPart.setContent(htmlContent, "text/html; charset=utf-8");
315
			htmlPart.setContent(htmlContent, "text/html; charset=utf-8");
253
			multipart.addBodyPart(htmlPart);
316
			multipart.addBodyPart(htmlPart);
Line 278... Line 341...
278
			while((line =bufferedReader.readLine())!=null){
341
			while((line =bufferedReader.readLine())!=null){
279
 
342
 
280
				htmlContent.append(line).append("\n");
343
				htmlContent.append(line).append("\n");
281
 
344
 
282
			}
345
			}
283
			Message msg = prepareMessage(recipients, subject, from, password);
346
			Message msg = prepareMessage(recipients,null, subject, from, password);
284
 
347
 
285
			//Create the multi-part object to hold the text and attachment parts
348
			//Create the multi-part object to hold the text and attachment parts
286
			Multipart multipart = new MimeMultipart();
349
			Multipart multipart = new MimeMultipart();
287
			
350
			
288
			MimeBodyPart htmlPart = new MimeBodyPart();
351
			MimeBodyPart htmlPart = new MimeBodyPart();
Line 322... Line 385...
322
		}
385
		}
323
	}
386
	}
324
 
387
 
325
 
388
 
326
	public static void main(String args[]) throws Exception {
389
	public static void main(String args[]) throws Exception {
-
 
390
		try {
327
		String[] sendTo = { "mandeep.dhir@shop2020.in", "mandeep.dhir@gmail.com", "mandeepcse07@yahoo.co.in", "kshitij.sood@shop2020.in" };
391
			String recipients[] = { "aman.kumar@shop2020.in", "amanshivam@gmail.com" };
328
		String emailSubjectTxt = "Another test from gmail";
392
			String cc[] = { "aman.kumar@saholic.com", "amanshivam11@gmail.com" };
-
 
393
			boolean debug = true;
-
 
394
 
-
 
395
			Properties props = new Properties();
-
 
396
			props.put("mail.smtp.host", SMTP_HOST_NAME);
-
 
397
			props.put("mail.smtp.auth", "true");
-
 
398
			props.put("mail.debug", "true");
-
 
399
			props.put("mail.smtp.port", SMTP_PORT);
-
 
400
			props.put("mail.smtp.socketFactory.port", SMTP_PORT);
-
 
401
			props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
-
 
402
			props.put("mail.smtp.socketFactory.fallback", "false");
-
 
403
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
-
 
404
 
-
 
405
			Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
-
 
406
 
-
 
407
				protected PasswordAuthentication getPasswordAuthentication() {
-
 
408
					return new PasswordAuthentication("adwords@shop2020.in", "adwords_shop2020");
-
 
409
				}
-
 
410
			});
-
 
411
 
-
 
412
			session.setDebug(debug);
-
 
413
 
329
		String emailMsgTxt = "Test Message Contents";
414
			Message msg = new MimeMessage(session);
-
 
415
 
-
 
416
			// Set the from address
330
		String emailFromAddress = "mandeep.dhir@shop2020.in";
417
			InternetAddress addressFrom = new InternetAddress("adwords@shop2020.in");
-
 
418
			msg.setFrom(addressFrom);
-
 
419
 
-
 
420
			// Set the recipients
-
 
421
			InternetAddress[] addressTo = new InternetAddress[recipients.length];
-
 
422
			for (int i = 0; i < recipients.length; i++) {
-
 
423
				addressTo[i] = new InternetAddress(recipients[i]);
-
 
424
			}
-
 
425
			msg.setRecipients(Message.RecipientType.TO, addressTo);
-
 
426
 
-
 
427
			if (cc != null) {
-
 
428
				// Set the cc
-
 
429
				InternetAddress[] addressCC = new InternetAddress[cc.length];
-
 
430
				for (int i = 0; i < cc.length; i++) {
-
 
431
					addressCC[i] = new InternetAddress(cc[i]);
-
 
432
				}
-
 
433
				msg.setRecipients(Message.RecipientType.CC, addressCC);
-
 
434
			}
-
 
435
 
331
		String password = "";
436
			// Setting the Subject
-
 
437
			msg.setSubject("subject abcd");
332
		String filename = "/home/mandeep/virtual_warehouse.sql";
438
			// Message msg = prepareMessage(recipients,cc, "abcd",
-
 
439
			// "adwords@shop2020.in", "adwords_shop2020");
333
 
440
 
-
 
441
			// Create the multi-part object to hold the text and attachment
-
 
442
			// parts
334
		GmailUtils utils = new GmailUtils();
443
			Multipart multipart = new MimeMultipart();
-
 
444
 
-
 
445
			// create the message part
335
		utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password, filename);
446
			MimeBodyPart messageBodyPart = new MimeBodyPart();
-
 
447
			// Part 1: Text message
336
		System.out.println("Sucessfully Sent mail to All Users");
448
			messageBodyPart.setText("this is a message");
-
 
449
			multipart.addBodyPart(messageBodyPart);
-
 
450
 
-
 
451
			// Put parts in message
-
 
452
			msg.setContent(multipart);
-
 
453
			
-
 
454
				
-
 
455
				Transport.send(msg);
-
 
456
			 
-
 
457
 
-
 
458
		} catch (Exception e) {
-
 
459
			e.printStackTrace();
-
 
460
		}
337
	}
461
	}
338
}
462
}