我尝试使用调度程序

时间:2015-05-04 05:44:08

标签: java session javamail

我要求在特定的时间间隔内自动从我的公司电子邮件帐户发送电子邮件。但即使我没有使用默认实例,我也会得到Access Denied to default session异常。不需要身份验证。请帮我解决此异常并定期发送邮件

MailServer.java

public class MailServer extends javax.mail.Authenticator
{
        private String mailhost  ="host";  //"smtp.mail.yahoo.com"; //"smtp.mail.com";
    final String username = "username";
final String password = "password";
   private Session session;  

   public MailServer(String user, String password) {


    Properties props = new Properties();
    props.put("mail.smtp.auth", "false");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "host");
    props.put("mail.smtp.port", "25");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, username);
        }
      });

    session = Session.getInstance(props, this);
}  

protected PasswordAuthentication getPasswordAuthentication()
{
    return new PasswordAuthentication(password, password);
}  

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
    MimeMessage message = new MimeMessage(session);
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
    message.setSender(new InternetAddress(sender));
    message.setSubject(subject);
    message.setDataHandler(handler);
    if (recipients.indexOf(',') > 0)

        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));

        else

        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

        Transport.send(message);
}  

.properties文件

setFrom = fgf.er@xxx.com
setPassword=xxx


#repeat for every 10 seconds
timetoquery = 10s

  #start after 2seconds for first time..
  delay = 2s

  emailTO = fgh.gm@xxx.com,fgh.hg@xxx.com

Testing.java

public class Testing extends TimerTask
 {

   public void run()
   {

               MailServer sender = new MailServer(Constants.setFrom, Constants.setPassword);

               try {
              sender.sendMail("Subject","This is Java4s",Constants.setFrom,Constants.emailTO);
        }
               catch (Exception e) {
             e.printStackTrace();
        }  

            System.out.println("Email Sent Succesfully...");

        }
}

CONSOLE

Scheduler started..
java.lang.SecurityException: Access to default session denied
at javax.mail.Session.getDefaultInstance(Session.java:292)
at javax.mail.Transport.send0(Transport.java:141)
at javax.mail.Transport.send(Transport.java:80)
at java4s.MailServer.sendMail(MailServer.java:63)
at java4s.Testing.run(Testing.java:15)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Email Sent Succesfully...

1 个答案:

答案 0 :(得分:6)

问题是你要隐藏一个变量:

Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, username);
    }
  });

session = Session.getInstance(props, this); // this is the local variable not the instance one

所以在sendMail时:

MimeMessage message = new MimeMessage(session); 

会话为null,java-mail尝试使用默认值。