Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.3 kB
1
Indexable
Never
    public String SendMail(String sender, String receiver, String subject, ArrayList<String> other, String mailText, boolean sendOhter) {
        String actionResult = "";
        System.out.println("Sending mail");
        Socket socket = null;
        ObjectInputStream inputStream = null;
        ObjectOutputStream outputStream;

        if (other.size() > 0 && sendOhter) {
            for (String otherDest : other) {
                System.out.println("DEST other: " + otherDest);

                ArrayList<String> otherCC = new ArrayList<>();
                for (String otherCCToAdd : other) {
                    if (!otherCCToAdd.equals(otherDest)) {
                        otherCC.add(otherCCToAdd);
                    }
                }
                otherCC.add(receiver);

                SendMail(sender, otherDest, "other: " + subject, otherCC, mailText, false);
            }
        }

        try {
            socket = new Socket(InetAddress.getLocalHost().getHostName(), 5689);
            System.out.println("Client connected to socket");

            outputStream = new ObjectOutputStream(socket.getOutputStream());

            List<Object> requestParams = new ArrayList<Object>();

            // Set email parameters
            requestParams.add(sender);
            requestParams.add(receiver);
            requestParams.add(subject);
            requestParams.add(other);
            requestParams.add(mailText);

            outputStream.writeObject(new Shared.Request("send_email", requestParams));

            inputStream = new ObjectInputStream(socket.getInputStream());
            actionResult = (String) inputStream.readObject();

            System.out.println("ACTION RESULT: " + actionResult);
        } catch (IOException e) {
            System.err.println("Err: new message send");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (socket != null && inputStream != null) {
                try {
                    inputStream.close();
                    socket.close();
                } catch (IOException e) {
                    System.err.println("Err: socket");
                }
            }
            return actionResult;
        }
    }