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 package de.saly.javamail.mock2;
27
28 import javax.mail.Address;
29 import javax.mail.Message;
30 import javax.mail.MessagingException;
31 import javax.mail.Session;
32 import javax.mail.Transport;
33 import javax.mail.URLName;
34 import javax.mail.event.ConnectionEvent;
35 import javax.mail.event.TransportEvent;
36 import javax.mail.internet.MimeMessage;
37
38 public class MockTransport extends Transport {
39
40 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
41
42 {
43 logger.warn("Mock Transport in use");
44 System.out.println("Mock Transport in use");
45 }
46
47 public MockTransport(final Session session, final URLName urlname) {
48 super(session, urlname);
49
50 }
51
52 @Override
53 public void connect(final String host, final int port, final String user, final String password) throws MessagingException {
54
55 if (isConnected()) {
56 throw new IllegalStateException("already connected");
57 }
58
59 setConnected(true);
60
61 notifyConnectionListeners(ConnectionEvent.OPENED);
62 }
63
64 @Override
65 public void sendMessage(final Message msg, final Address[] addresses) throws MessagingException {
66 for (final Address a : addresses) {
67 final MockMailbox mailbox = MockMailbox.get(a);
68 if (mailbox.getInbox().isSimulateError()) {
69
70 notifyTransportListeners(TransportEvent.MESSAGE_NOT_DELIVERED, new Address[0], addresses, new Address[0], msg);
71
72 throw new MessagingException("Simulated error sending message to " + a);
73
74 }
75
76 mailbox.getInbox().add(new MimeMessage((MimeMessage) msg));
77 notifyTransportListeners(TransportEvent.MESSAGE_DELIVERED, addresses, new Address[0], new Address[0], msg);
78 }
79 }
80
81 }