Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define the root logger with file appender
- log4j.rootLogger = debug, sql, stdout
- #log4j.rootLogger = error, sql, stdout
- # Log for the Hibernate
- log4j.logger.org.hibernate=ERROR
- #log4j.logger.org.hibernate=INFO
- #log4j.logger.org.hibernate.type=trace
- #log4j.logger.org.hibernate.type=ALL
- # ================ Added as part of logging into DB ====================
- # Define the jdbc appender
- log4j.appender.sql=org.apache.log4j.jdbc.JDBCAppender
- log4j.appender.sql.URL=jdbc:sqlserver://localhost:1433;DatabaseName=ERP_IDMS
- # Set Database Driver
- log4j.appender.sql.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
- # Set database user name and password
- log4j.appender.sql.user=ERP_ADMIN_IDMS
- log4j.appender.sql.password=Password
- # Set the SQL statement to be executed.
- #short
- log4j.appender.sql.layout.ConversionPattern=INSERT INTO LOGS1 VALUES ('%d{yyyy-MM-dd HH:mm:ss.SSS}' ,'%C','%p','%m', '%throwable{short}', '%throwable{800}')
- # Define the xml layout for file appender
- log4j.appender.sql.layout=org.apache.log4j.EnhancedPatternLayout
- #=======================================================================
- # Direct to stdout
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target=System.out
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
- # =============================================================================
- USE [ERP_IDMS]
- GO
- /****** Object: Table [ERP_ADMIN].[LOGS1] Script Date: 01/18/2016 16:55:44 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [ERP_ADMIN].[LOGS1](
- [DATED] [datetime] NOT NULL,
- [LOGGER] [varchar](500) NOT NULL,
- [LEVEL] [varchar](200) NOT NULL,
- [MESSAGE] [varchar](1500) NOT NULL,
- [THROWABLE] [varchar](150) NULL,
- [STACKTRACE] [varchar](5000) NULL
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- package erp.idms.test;
- import java.util.Date;
- //import java.util.HashMap;
- //import java.util.List;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- //import javax.mail.BodyPart;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import org.apache.log4j.Logger;
- import com.erp.idms.mail.exception.EMailException;
- import com.erp.idms.mail.exception.SMTPNotFoundException;
- /*import erp.idms.mail.exception.*;
- import erp.idms.mail.vo.EmailServiceRequestVO;
- import erp.idms.mail.vo.EmailServiceResponseVO;
- import erp.idms.modal.MailingList;
- import erp.idms.test.MailTest;*/
- public class MailTest {
- private static Logger logger = Logger.getLogger(MailTest.class);
- public static void main(String[] args) {
- MailTest test = new MailTest();
- try {
- test.sendMail("Hi n this is a test mail");
- } catch (EMailException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void sendMail(String msg) throws EMailException {
- Transport transport = null;
- String transportStr = "smtp";
- String contentStr = "text/plain";
- String smtpHostStr = "mail.smtp.host";
- String smtpHost = "192.85.144.90";
- //138.35.160.239
- //"192.85.142.93"
- String smtpHost1 = "smtp.svcs.hpe.com";
- final String username = "mukesh-kumarp@hp.com";// change accordingly
- final String password = "Sona@123";// change accordingly
- String responseStatus = null;
- String message_header = null;
- String message_footer = null;
- // Recipient's email ID needs to be mentioned.
- String to = "mukesh-kumarp@hpe.com";//anil.veldandi@hp.com
- // Sender's email ID needs to be mentioned
- //"mcbc_apc_dataload@hpe.com"
- //"apc_dataload@hp.com"
- String from = "mcbc_apc_dataload@hpe.com";
- String from1 = "mukesh-kumarp@hp.com";
- Session session = null;
- try {
- Properties props = new Properties();
- if (username != null) {
- props.put("mail.smtp.auth", "true");
- }
- props.put(smtpHostStr, smtpHost);
- // Get the Session object.
- session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username,
- password);
- }
- });
- session.setDebug(false);
- // Create a default MimeMessage object.
- MimeMessage mimeMsg = new MimeMessage(session);
- // Set From: header field of the header.
- mimeMsg.setFrom(new InternetAddress(from));
- // Set To: header field of the header.
- mimeMsg.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(to));
- // Setting the "CC recipients" addresses if exists
- String ccAddress = null;
- if (ccAddress != null && !"".equals(ccAddress)) {
- mimeMsg.setRecipients(Message.RecipientType.CC, ccAddress);
- }
- // Set Subject: header field
- mimeMsg.setSubject("Testing Subject");
- // Create the message part
- MimeBodyPart mimeBodyPart = new MimeBodyPart();
- // Now set the actual message
- mimeBodyPart.setText(msg);
- // Create a multipar message
- Multipart multipart = new MimeMultipart();
- // Set text message part
- multipart.addBodyPart(mimeBodyPart);
- // Part two is attachment
- //mimeBodyPart = new MimeBodyPart();
- String[] fileToBeAttached = new String[3];
- fileToBeAttached = null;
- //fileToBeAttached[0] = "C:\ERP_Automation\OutBoundDir\outBound.csv";
- //fileToBeAttached[1] = "C:\IDMS_ERP_DataAutomation\Invoice.log";
- //fileToBeAttached[2] = "C:\IDMS_ERP_DataAutomation\Invoice.bad";
- if (fileToBeAttached != null && fileToBeAttached.length > 0) {
- for (String filenameAttach : fileToBeAttached) {
- MimeBodyPart attachPart = new MimeBodyPart();
- DataSource source = new FileDataSource(filenameAttach);
- attachPart.setDataHandler(new DataHandler(source));
- String fileName = filenameAttach
- .substring(filenameAttach.lastIndexOf("\")+1);
- // mimeBodyPart = new MimeBodyPart();
- attachPart.setFileName(fileName);
- // Send the complete message parts
- multipart.addBodyPart(attachPart);
- }
- }
- mimeMsg.setContent(multipart);
- // Set the Date: header
- mimeMsg.setSentDate(new Date());
- mimeMsg.saveChanges();
- // Send message
- // Transport.send(mimeMsg);
- transport = session.getTransport(transportStr);
- System.out.println("----connecting Transport ----");
- logger.info("----connecting Transport ----");
- transport.connect();
- System.out.println(mimeMsg.getAllRecipients());
- transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
- System.out.println("Sent message successfully....");
- logger.info("Sent message successfully....");
- }/* catch (MessagingException e) {
- //e.printStackTrace();
- logger.error("Exception while sending the Message"+e);
- throw new SMTPNotFoundException("Unable to send the Mail", e);
- }*/ catch (Exception e) {
- e.printStackTrace();
- logger.error("Exception while sending the Message",e);
- throw new RuntimeException(e);
- }
- }
- private static class SMTPAuth extends Authenticator {
- private String user;
- private String pass;
- /**
- * @param u
- * @param p
- */
- public SMTPAuth(String username, String password) {
- user = username;
- pass = password;
- }
- /* (non-Javadoc)
- * @see javax.mail.Authenticator#getPasswordAuthentication()
- */
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user,pass);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement