aboutsummaryrefslogtreecommitdiff
path: root/test/org/traccar/notification/NotificiationMailTest.java
blob: 9405ff94f06df8816d2a9d2e783a158b302d300d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.traccar.notification;

import org.junit.Assert;
import org.junit.Test;
import org.traccar.location.CellInfo;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class NotificiationMailTest {

    private static final String FROM = "notification@traccar.org";
    private static final String TO = "anton.tananaev@gmail.com";

    private static final String BODY = "Test email body.";
    private static final String SUBJECT = "Test";

    private static final String SMTP_USERNAME = "username";
    private static final String SMTP_PASSWORD = "password";

    private static final String HOST = "email-smtp.us-west-2.amazonaws.com";

    private static final int PORT = 25;

    @Test
    public void test() throws Exception {

        Properties props = System.getProperties();
        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtp.port", PORT);

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");

        Session session = Session.getInstance(props);

        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setContent(BODY,"text/plain");

        Transport transport = session.getTransport();

        try
        {
            transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
            transport.sendMessage(msg, msg.getAllRecipients());
        } finally {
            transport.close();
        }

    }

}