OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 182
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.utils.io;
14
 package org.openconcerto.utils.io;
15
 
15
 
16
import org.openconcerto.utils.ExceptionUtils;
16
import org.openconcerto.utils.ExceptionUtils;
17
 
17
 
-
 
18
import java.util.Objects;
18
import java.util.Properties;
19
import java.util.Properties;
19
 
20
 
20
import javax.mail.Address;
21
import javax.mail.Address;
21
import javax.mail.Message;
22
import javax.mail.Message;
22
import javax.mail.MessagingException;
23
import javax.mail.MessagingException;
23
import javax.mail.PasswordAuthentication;
24
import javax.mail.PasswordAuthentication;
24
import javax.mail.SendFailedException;
25
import javax.mail.SendFailedException;
25
import javax.mail.Transport;
26
import javax.mail.Transport;
-
 
27
import javax.mail.internet.AddressException;
-
 
28
import javax.mail.internet.InternetAddress;
26
 
29
 
27
/**
30
/**
28
 * A mail account.
31
 * A mail account.
29
 * 
32
 * 
30
 * @author Sylvain
33
 * @author Sylvain
Line 105... Line 108...
105
        } while (ex != null);
108
        } while (ex != null);
106
 
109
 
107
        return sb.toString();
110
        return sb.toString();
108
    }
111
    }
109
 
112
 
-
 
113
    public static final MailAccount create(final String fromAddr, String smtpServer, String smtpLogin, String smtpPassword) throws AddressException {
-
 
114
        Objects.requireNonNull(fromAddr, "Missing 'From:' address");
-
 
115
        final InternetAddress fromInetAddr = new InternetAddress(fromAddr, true);
-
 
116
        if (smtpServer == null) {
-
 
117
            smtpServer = "smtp." + fromInetAddr.getAddress().substring(fromInetAddr.getAddress().indexOf('@') + 1);
-
 
118
        }
-
 
119
        if (smtpLogin == null) {
-
 
120
            smtpLogin = fromInetAddr.getAddress().substring(0, fromInetAddr.getAddress().indexOf('@'));
-
 
121
        }
-
 
122
        final MailAccount res = new MailAccount(fromInetAddr.getPersonal(), fromInetAddr.getAddress(), smtpServer);
-
 
123
        res.setAuth(new PasswordAuthentication(smtpLogin, smtpPassword));
-
 
124
        return res;
-
 
125
    }
-
 
126
 
110
    // nullable
127
    // nullable
111
    private final String name;
128
    private final String name;
112
    private final String address;
129
    private final String address;
113
    private final String smtpServer;
130
    private final String smtpServer;
114
    private final int port;
131
    private final int port;
Line 180... Line 197...
180
        return props;
197
        return props;
181
    }
198
    }
182
 
199
 
183
    public final void send(final Message msg) throws MessagingException {
200
    public final void send(final Message msg) throws MessagingException {
184
        final PasswordAuthentication auth = this.getAuth();
201
        final PasswordAuthentication auth = this.getAuth();
-
 
202
        final Properties props = msg.getSession().getProperties();
185
        msg.getSession().getProperties().setProperty("mail." + getSMTPProtocol() + ".auth", auth == null ? "false" : "true");
203
        props.setProperty("mail." + getSMTPProtocol() + ".auth", auth == null ? "false" : "true");
186
        try (final Transport mailTransport = msg.getSession().getTransport()) {
204
        try (final Transport mailTransport = msg.getSession().getTransport()) {
187
            if (auth == null)
205
            if (auth == null)
188
                mailTransport.connect();
206
                mailTransport.connect();
189
            else
207
            else
190
                mailTransport.connect(auth.getUserName(), auth.getPassword());
208
                mailTransport.connect(auth.getUserName(), auth.getPassword());
191
            mailTransport.sendMessage(msg, msg.getAllRecipients());
209
            mailTransport.sendMessage(msg, msg.getAllRecipients());
-
 
210
        } catch (MessagingException e) {
-
 
211
            throw new MessagingException("Couldn't send as " + auth.getUserName() + " using\n" + props, e);
192
        }
212
        }
193
    }
213
    }
194
 
214
 
195
    @Override
215
    @Override
196
    public String toString() {
216
    public String toString() {