Log4j exception when sending mail using plain java mail

I already read this Simple Java Mail wiki tutorial, and I downloaded all the necessary libraries (Log4j, JavaMail API, Activation Environment) although when I try to run my program I get this error:

log4j: WARN No applications were found for the logger (org.codemonkey.vesijama.Mailer). org.codemonkey.vesijama.MailException: general error: log4j exception read response: WARN Please initialize the log4j system correctly.

This is the source code I'm using:

import javax.mail.Message.RecipientType; import org.codemonkey.vesijama.Email; import org.codemonkey.vesijama.MailException; import org.codemonkey.vesijama.Mailer; import org.apache.log4j.*; public class testSend { final Email email = new Email(); static Logger log = Logger.getLogger(mailmailan.class); public testSend{ try{ BasicConfigurator.configure(); email.setFromAddress("test", " XXXXX@gmail.com "); email.setSubject("hey"); email.addRecipient("hai", " XXXXXXX@yahoo.com ", RecipientType.TO); email.setText("We should meet up!"); email.setTextHTML("<b>We should meet up!</b>"); email.addAttachment("output.xls", odfDatasource); new Mailer("smtp.gmail.com", 465, " XXXXXXXX@gmail.com ", "XXXXXX").sendMail(email); } catch(MailException me) { System.out.println(me); } } } 

I also tried using port 587, although I got the same error.

Side question: It also says that you can add attachments. Does anyone have sample code on how I can attach a .xls file.

Edit: I have successfully sent mail (added log4j.xml for each folder), but I still could not use addAttachment . I also updated the source code.

+4
source share
1 answer

The first error you get is related to the fact that the log4j.xml file could not be found in the class path to configure the log4j log.

Solving this problem should give you more protocols to help you solve any other remaining problems.

You will need to create a log4j configuration file to see the result.

Googling for "plain log4j.xml" quickly found this example:

 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.SimpleLayout"/> </appender> <category name="com.something.something.you.want.to.see.debug"> <priority value="debug"/> </category><root><priority value="info"/> <appender-ref ref="ConsoleAppender"/> </root> </log4j:configuration> 

These are messages for all log and debug messages for the data specified in the named category (I called "com.something.something.you.want.to.see.debug"). Usually this name matches your package structure,

Search the log4j tutorials for more log4j configuration options.

+1
source

Source: https://habr.com/ru/post/1310518/


All Articles