开发者

Log4j exception when sending mail with simple-java-mail

I already read this tutorial on the Simple Java Mail wiki and I have downloaded all required libraries (Log4j, JavaMail API, Activation framework) although when I try running my program I get this error:

log4j:WARN No appenders could be found for logger (org.codemonkey.vesijama.Mailer). org.codemonkey.vesijama.MailException: Generic error: Exception reading response log4j:WARN Please initialize the log4j system properly.

This is the source code i use:

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").sendMa开发者_开发问答il(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 it is possible to add attachments. Does anyone have a code example of how I can attach a .xls file.

Edit: I have success sent mail (Added log4j.xml to every folder), but i still failed to use addAttachment. I have also updated my source code.


The first error you are getting is related to not being able to find a log4j.xml file in the classpath to configure log4j logging.

Solving that problem should give you more logging to help you solve any other remaining problems.

You'll need to create a log4j configuration file to see the output.

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

<?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>

This logs info messages for everything, and debug for stuff specified in the named category (I called "com.something.something.you.want.to.see.debug") Typically this name is the same as your package structure.

Search for log4j tutorials to learn more options for log4j configuration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜