通过Javamail在MySql表中存储数据失败

Dus*_*usk 8 java mysql jakarta-mail james

如何将我通过javamail编写的消息存储到MySQL表中?我已经配置james服务器配置文件连接到MySQL服务器(数据源元素名称为maildb),我将<inboxRepository>James服务器配置文件中的元素更改为

<inboxRepository>
  <repository destinationURL="db://maildb/spammer/"
    type="MAIL"/>      
</inboxRepository>
Run Code Online (Sandbox Code Playgroud)

但是我仍然无法从MySql中的邮件数据库中的垃圾邮件发送者表的收件箱列中读取邮件.

这是我的javamail类:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class mail{

  public static void main(String[] argts){
    String to = "red@localhost";
    String from = "blue@localhost";
    String subject = "jdk";
    String body = "Down to wind";

    if ((from != null) && (to != null) 
      && (subject != null)  && (body != null)) 
    // we have mail to send
    {
      try {
        Properties props = new Properties();

        props.put("mail.host", "127.0.0.1 ");
        props.put("mail.smtp.auth","true");

        Session session = 
          Session.getInstance(props, new javax.mail.Authenticator() {

          protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("blue", "blue");
          }
        });
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        Address[] add={ new InternetAddress(to) };
        message.setRecipients(Message.RecipientType.TO,add);
        message.setSubject(subject);
        message.setContent(body, "text/plain");
        message.setText(body);
        Transport.send(message);

        System.out.println
          ("<b>Thank you. Your message to "+to+" was successfully sent.</b>");

      } catch (Throwable t) {
        t.printStackTrace();
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么,如何从MySQL中的垃圾邮件表中读取消息?

小智 1

也许您使用了错误的数据库 URL:destinationURL="db://maildb/spammer/" 当然,如果目标是 mysql 数据库,我建议更改为 destinationURL="mysql://maildb/spammer/"。