Ejemplo n.º 1
0
  public MailClient fromProperties(String name) {
    File file = new File(name);
    File constantsFiles = new File("mailconstants.properties");
    if (!file.exists()) return null;
    else {
      Properties props = new Properties();

      try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedInputStream cis = new BufferedInputStream(new FileInputStream(constantsFiles));

        props.load(bis);
        props.load(cis);
        IOUtils.closeQuietly(bis);
        Server defaultIncoming = new Server();
        String type = props.getProperty("mail.incoming.type");
        String address = props.getProperty("mail.incoming.address");
        String defaultPort =
            type.contains("imaps")
                ? "993"
                : type.contains("imap")
                    ? "143"
                    : type.contains("pops") ? "996" : type.contains("pop") ? "110" : null;
        String port = props.getProperty("mail.incoming.port", defaultPort);
        Integer num = Integer.parseInt(port);
        String serverName = props.getProperty("mail.incoming.name");
        defaultIncoming.setPort(num);
        defaultIncoming.setServerAddress(address);
        defaultIncoming.setServerName(serverName);
        defaultIncoming.setServerType(type);
        List<Server> incoming = Collections.singletonList(defaultIncoming);

        SMTPServer defaultOutgoing = new SMTPServer();
        String outgoingType = props.getProperty("mail.outgoing.type");
        String outgoingAddress = props.getProperty("mail.outgoing.address");
        String outgoingPort = type.contains("smtps") ? "465" : type.contains("smtp") ? "25" : null;
        String outPort = props.getProperty("mail.outgoing.port", outgoingPort);
        Integer numOut = Integer.parseInt(outPort);
        String outName = props.getProperty("mail.outgoing.name");
        defaultOutgoing.setPort(numOut);
        defaultOutgoing.setServerAddress(outgoingAddress);
        defaultOutgoing.setServerName(outName);
        defaultOutgoing.setServerType(outgoingType);
        List<SMTPServer> outgoing = Collections.singletonList(defaultOutgoing);

        MailStore store = new DefaultMailStore();
        store.setIncomingServers(incoming);
        store.setOutGoingServers(outgoing);

        MailBox mailBox = new DefaultMailBox();
        MailClient ret = new DefaultMailClient(mailBox, store);
        return ret;

      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return null;
  }