commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gaurav.c...@ubs.com
Subject Problem with sending email to a Distribution list
Date Fri, 28 Sep 2007 20:36:03 GMT
I am using the Common Email API , I am having problem while sending the
email to our distribution list . Though it work well when I send to a
particular user .

Thanks


/*
 * Created on Sep 25, 2007
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.ubs.ecl.batch.vendor;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

import com.pwj.fwa.log4j.PwLogger;
import
com.ubs.ecl.batch.vendor.datamanager.FulfillmentVendorDB2DataManager;
import com.ubs.ecl.bean.PendingFullfillmentBean;
import com.ubs.ecl.common.AIDataUtil;
import com.ubs.ecl.common.AIException;
import com.ubs.ecl.common.ECLConstants;
import com.ubs.ecl.common.config.AIConfig;

/**
 * @author ge01969
 * This class will generate an email to vendor daily (depends on
vendor's requirement)
 * to display the list of request(control #) which are pending to
fullfill.
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class PendingFullfillmentNotificationModel {
	private static final PwLogger logger =
PwLogger.getLogger(PendingFullfillmentNotificationModel.class);
	
	private List getPendingFullfillmentRequest(String vendorAcronym)
throws AIException {
		FulfillmentVendorDB2DataManager dataManager = new
FulfillmentVendorDB2DataManager(vendorAcronym);
		List pendingList = null;
		try {
			pendingList =
dataManager.getPendingFullfillmentRequest(vendorAcronym);
		} catch (AIException e) {
			logger.error("There is some problem while
getting the pending fullfillment request list");
			throw e;
		}
		return pendingList;
	}
	
	public boolean sendVendorNotification(String vendorAcronym)
throws AIException  {
		List list = new ArrayList();
		boolean isSent = false;
			try {
				list =
getPendingFullfillmentRequest(vendorAcronym);
			} catch (AIException e1) {
				e1.printStackTrace();
			}
	
		int size = list.size();
	
		String to =
AIConfig.getString("vendor.notification."+vendorAcronym+".list");
		List toList = getReceiverList(to,";");
		String subject =
AIConfig.getString("vendor.notification.subject")+" - " +vendorAcronym;
		//System.out.println(subject);
		String cc =
AIConfig.getString("business.notification.list.cc");
		List ccList = getReceiverList(cc,";");
		
		
		StringBuffer messageBuffer = new StringBuffer();
		String message = "<HTML><BODY>";
		PendingFullfillmentBean pendingFullfillment = null;
		
		if(list.isEmpty()){
			message = message
+AIConfig.getString("vendor.notification.header1")+ " "+"<B>"+size +
"</B>"+" "+
	
AIConfig.getString("vendor.notification.header2")+"<BR><BR>";
			messageBuffer.append(message);	 
		}
		else {
				message = message
+AIConfig.getString("vendor.notification.header1")+ " "+"<B>"+size +
"</B>"+" "+
	
AIConfig.getString("vendor.notification.header2")+"<BR>";
				messageBuffer.append(message);	 
				messageBuffer.append("<BR>");
				messageBuffer.append("<table
border='1'>");
				messageBuffer.append("<tr colspan=7><td
colspan=1>Item # ");
				messageBuffer.append("<td
colspan=3>Control Numbers </td>");
				messageBuffer.append("<td colspan=3>Last
Updated Date</td>");
				//messageBuffer.append("<ol
start='1'>");
				int rowCount = 0;
							 
				for(int i=0;i<size ;i++){
					messageBuffer.append("<tr
colspan=5>");
					rowCount = i+1;
					pendingFullfillment =
(PendingFullfillmentBean)list.get(i);
					messageBuffer.append("<td
colspan=1>"+rowCount +"</td>");
					messageBuffer.append("<td
colspan=3>"+pendingFullfillment.getControlNumber()+"</td>");
					messageBuffer.append("<td
colspan=3>"+AIDataUtil.formatStringDate(pendingFullfillment.getUpdatedDa
te(),ECLConstants.DATE_FORMAT)+"</td>");
	
//messageBuffer.append(pendingFullfillment.getControlNumber());
					messageBuffer.append("</tr>");
					 
				}
				//messageBuffer.append("</ol>");
				messageBuffer.append("</table>");
	
messageBuffer.append("<BR>"+AIConfig.getString("vendor.production.contac
t"));
				messageBuffer.append("</BODY></HTML>");
			}
		   // now set the property to email and send 
		   System.out.println(messageBuffer.toString());
		   try {
				SimpleEmail email = new SimpleEmail();
//			Create properties for the Session
		 	//Properties props = new Properties();

//			If using static Transport.send(),
//			need to specify the mail server here
		 	//props.put("mail.smtp.host",
AIConfig.getString("smtphost"));
//			To see what is going on behind the scene
		 	//props.put("mail.debug", "true");

//			Get a session
		 	//Session session = Session.getInstance(props);
		 	//session.
			//Transport bus = session.getTransport("smtp");
			//Message b = new MimeMessage(session);
			//b.setText("Test Message");

			//bus.connect();
			//InternetAddress addr = new
InternetAddress("DL-ECL-SUPPORT@ubs.com");
			//InternetAddress addr[] =
InternetAddress.parse("DL-ECL-SUPPORT@ubs.com",true);
			//for(int i=0;i<addr.length;i++){
	
//System.out.println(addr[i].getAddress());
			//}
			//bus.send(b,addr);


				//javax.mail.Session session =
getInstance(new Properties());
				email.setDebug(true);
				//Email mail = new Email();
				//mail.
	
email.setFrom(AIConfig.getString("sender.email"),"Neeraj,Verma");
				email.setPopBeforeSmtp(true,"","","");
				email.setTo(toList);
				//email.setCc(ccList);
	
email.setBounceAddress(AIConfig.getString("vendor.bounce.address"));
				
	
email.setHostName(AIConfig.getString("smtphost"));
				
				email.setSubject(subject);
				email.addReplyTo("sender.email");
				email.setMsg(messageBuffer.toString());
				
				email.send();
				isSent = true;
			} catch (EmailException e) {
				logger.error("There is some problem
while sending an email to the vendor");
				e.printStackTrace();
				throw new AIException(e);
		   }	
//			} catch (NoSuchProviderException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (MessagingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} 
//			catch (NoSuchProviderException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (MessagingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		
		return isSent;
	}
	
	private List getReceiverList(String address,String delimiter) {
		List l = null;
		InternetAddress emailId = null;
		if(address!=null){
			l= new ArrayList();
		    StringTokenizer str = new
StringTokenizer(address,delimiter,false);
			while (str.hasMoreTokens()){
				String toString
=(String)str.nextToken();
				try {
					emailId = new
InternetAddress(toString);
					InternetAddress[] addr
=emailId.parse(toString,true);
					int k = addr.length;
					System.out.println(addr);
					l.add(emailId);
				} catch (AddressException e) {
					logger.error("There is some
problem while checking the email address");
					e.printStackTrace();
				} 
			}
		}
		return l;
	}
	
	
	
	
}


Thanks
Gaurav

Thanks
Gaurav S. Chug
Investment Solutions Technology
UBS Financial Services, Inc.
1000 Harbor Blvd / 3rd Floor
Weehawken, NJ 07086
1-201-352-9579
gaurav.chug@ubs.com


























Please do not transmit orders or instructions regarding a UBS account by e-mail. The information
provided in this e-mail or any attachments is not an official transaction confirmation or
account statement. For your protection, do not include account numbers, Social Security numbers,
credit card numbers, passwords or other non-public information in your e-mail. Because the
information contained in this message may be privileged, confidential, proprietary or otherwise
protected from disclosure, please notify us immediately by replying to this message and deleting
it from your computer if you have received this communication in error. Thank you.



UBS Financial Services Inc.

UBS International Inc.

UBS Financial Services Incorporated of Puerto Rico

Mime
  • Unnamed multipart/alternative (inline, 7-Bit, 0 bytes)
View raw message