· 10 years ago · Jan 18, 2016, 11:39 AM
1# Define the root logger with file appender
2log4j.rootLogger = debug, sql, stdout
3#log4j.rootLogger = error, sql, stdout
4
5# Log for the Hibernate
6log4j.logger.org.hibernate=ERROR
7#log4j.logger.org.hibernate=INFO
8#log4j.logger.org.hibernate.type=trace
9#log4j.logger.org.hibernate.type=ALL
10
11# ================ Added as part of logging into DB ====================
12# Define the jdbc appender
13log4j.appender.sql=org.apache.log4j.jdbc.JDBCAppender
14log4j.appender.sql.URL=jdbc:sqlserver://localhost:1433;DatabaseName=ERP_IDMS
15
16# Set Database Driver
17log4j.appender.sql.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
18
19# Set database user name and password
20log4j.appender.sql.user=ERP_ADMIN_IDMS
21log4j.appender.sql.password=Password
22
23# Set the SQL statement to be executed.
24#short
25log4j.appender.sql.layout.ConversionPattern=INSERT INTO LOGS1 VALUES ('%d{yyyy-MM-dd HH:mm:ss.SSS}' ,'%C','%p','%m', '%throwable{short}', '%throwable{800}')
26# Define the xml layout for file appender
27log4j.appender.sql.layout=org.apache.log4j.EnhancedPatternLayout
28#=======================================================================
29
30# Direct to stdout
31log4j.appender.stdout=org.apache.log4j.ConsoleAppender
32log4j.appender.stdout.Target=System.out
33log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
34log4j.appender.stdout.layout.ConversionPattern=d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
35
36# =============================================================================
37
38USE [ERP_IDMS]
39GO
40/****** Object: Table [ERP_ADMIN].[LOGS1] Script Date: 01/18/2016 16:55:44 ******/
41SET ANSI_NULLS ON
42GO
43SET QUOTED_IDENTIFIER ON
44GO
45SET ANSI_PADDING ON
46GO
47CREATE TABLE [ERP_ADMIN].[LOGS1](
48 [DATED] [datetime] NOT NULL,
49 [LOGGER] [varchar](500) NOT NULL,
50 [LEVEL] [varchar](200) NOT NULL,
51 [MESSAGE] [varchar](1500) NOT NULL,
52 [THROWABLE] [varchar](150) NULL,
53 [STACKTRACE] [varchar](5000) NULL
54) ON [PRIMARY]
55
56GO
57SET ANSI_PADDING OFF
58
59package erp.idms.test;
60
61import java.util.Date;
62//import java.util.HashMap;
63//import java.util.List;
64import java.util.Properties;
65import javax.activation.DataHandler;
66import javax.activation.DataSource;
67import javax.activation.FileDataSource;
68import javax.mail.Authenticator;
69//import javax.mail.BodyPart;
70import javax.mail.Message;
71import javax.mail.MessagingException;
72import javax.mail.Multipart;
73import javax.mail.PasswordAuthentication;
74import javax.mail.Session;
75import javax.mail.Transport;
76import javax.mail.internet.InternetAddress;
77import javax.mail.internet.MimeBodyPart;
78import javax.mail.internet.MimeMessage;
79import javax.mail.internet.MimeMultipart;
80
81import org.apache.log4j.Logger;
82
83
84import com.erp.idms.mail.exception.EMailException;
85import com.erp.idms.mail.exception.SMTPNotFoundException;
86
87
88
89/*import erp.idms.mail.exception.*;
90import erp.idms.mail.vo.EmailServiceRequestVO;
91import erp.idms.mail.vo.EmailServiceResponseVO;
92import erp.idms.modal.MailingList;
93import erp.idms.test.MailTest;*/
94
95public class MailTest {
96
97 private static Logger logger = Logger.getLogger(MailTest.class);
98
99 public static void main(String[] args) {
100
101 MailTest test = new MailTest();
102 try {
103 test.sendMail("Hi n this is a test mail");
104 } catch (EMailException e) {
105 // TODO Auto-generated catch block
106 e.printStackTrace();
107 }
108 }
109
110 public void sendMail(String msg) throws EMailException {
111
112 Transport transport = null;
113 String transportStr = "smtp";
114 String contentStr = "text/plain";
115 String smtpHostStr = "mail.smtp.host";
116
117 String smtpHost = "192.85.144.90";
118 //138.35.160.239
119 //"192.85.142.93"
120 String smtpHost1 = "smtp.svcs.hpe.com";
121 final String username = "mukesh-kumarp@hp.com";// change accordingly
122 final String password = "Sona@123";// change accordingly
123 String responseStatus = null;
124 String message_header = null;
125 String message_footer = null;
126
127 // Recipient's email ID needs to be mentioned.
128 String to = "mukesh-kumarp@hpe.com";//anil.veldandi@hp.com
129
130
131 // Sender's email ID needs to be mentioned
132 //"mcbc_apc_dataload@hpe.com"
133 //"apc_dataload@hp.com"
134 String from = "mcbc_apc_dataload@hpe.com";
135 String from1 = "mukesh-kumarp@hp.com";
136
137 Session session = null;
138
139 try {
140 Properties props = new Properties();
141 if (username != null) {
142 props.put("mail.smtp.auth", "true");
143 }
144 props.put(smtpHostStr, smtpHost);
145
146 // Get the Session object.
147 session = Session.getInstance(props,
148 new javax.mail.Authenticator() {
149 protected PasswordAuthentication getPasswordAuthentication() {
150 return new PasswordAuthentication(username,
151 password);
152 }
153 });
154 session.setDebug(false);
155 // Create a default MimeMessage object.
156 MimeMessage mimeMsg = new MimeMessage(session);
157
158 // Set From: header field of the header.
159 mimeMsg.setFrom(new InternetAddress(from));
160
161 // Set To: header field of the header.
162 mimeMsg.setRecipients(Message.RecipientType.TO,
163 InternetAddress.parse(to));
164 // Setting the "CC recipients" addresses if exists
165 String ccAddress = null;
166 if (ccAddress != null && !"".equals(ccAddress)) {
167 mimeMsg.setRecipients(Message.RecipientType.CC, ccAddress);
168 }
169
170 // Set Subject: header field
171 mimeMsg.setSubject("Testing Subject");
172
173 // Create the message part
174 MimeBodyPart mimeBodyPart = new MimeBodyPart();
175
176 // Now set the actual message
177 mimeBodyPart.setText(msg);
178
179 // Create a multipar message
180 Multipart multipart = new MimeMultipart();
181
182 // Set text message part
183 multipart.addBodyPart(mimeBodyPart);
184
185 // Part two is attachment
186 //mimeBodyPart = new MimeBodyPart();
187 String[] fileToBeAttached = new String[3];
188 fileToBeAttached = null;
189 //fileToBeAttached[0] = "C:\ERP_Automation\OutBoundDir\outBound.csv";
190 //fileToBeAttached[1] = "C:\IDMS_ERP_DataAutomation\Invoice.log";
191 //fileToBeAttached[2] = "C:\IDMS_ERP_DataAutomation\Invoice.bad";
192
193 if (fileToBeAttached != null && fileToBeAttached.length > 0) {
194
195 for (String filenameAttach : fileToBeAttached) {
196 MimeBodyPart attachPart = new MimeBodyPart();
197 DataSource source = new FileDataSource(filenameAttach);
198 attachPart.setDataHandler(new DataHandler(source));
199 String fileName = filenameAttach
200 .substring(filenameAttach.lastIndexOf("\")+1);
201 // mimeBodyPart = new MimeBodyPart();
202 attachPart.setFileName(fileName);
203
204 // Send the complete message parts
205 multipart.addBodyPart(attachPart);
206
207 }
208
209 }
210
211
212 mimeMsg.setContent(multipart);
213 // Set the Date: header
214 mimeMsg.setSentDate(new Date());
215
216 mimeMsg.saveChanges();
217
218 // Send message
219 // Transport.send(mimeMsg);
220
221 transport = session.getTransport(transportStr);
222 System.out.println("----connecting Transport ----");
223 logger.info("----connecting Transport ----");
224 transport.connect();
225 System.out.println(mimeMsg.getAllRecipients());
226 transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
227
228 System.out.println("Sent message successfully....");
229 logger.info("Sent message successfully....");
230
231 }/* catch (MessagingException e) {
232 //e.printStackTrace();
233 logger.error("Exception while sending the Message"+e);
234 throw new SMTPNotFoundException("Unable to send the Mail", e);
235 }*/ catch (Exception e) {
236 e.printStackTrace();
237 logger.error("Exception while sending the Message",e);
238 throw new RuntimeException(e);
239 }
240 }
241
242
243
244
245 private static class SMTPAuth extends Authenticator {
246 private String user;
247 private String pass;
248
249 /**
250 * @param u
251 * @param p
252 */
253 public SMTPAuth(String username, String password) {
254 user = username;
255 pass = password;
256 }
257
258 /* (non-Javadoc)
259 * @see javax.mail.Authenticator#getPasswordAuthentication()
260 */
261 protected PasswordAuthentication getPasswordAuthentication() {
262 return new PasswordAuthentication(user,pass);
263 }
264 }
265
266
267
268}