com.icegreen.greenmail.smtp.SmtpServer Java Examples

The following examples show how to use com.icegreen.greenmail.smtp.SmtpServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EMailTestServer.java    From syndesis with Apache License 2.0 6 votes vote down vote up
public int getEmailCountInFolder(String user, String password, String folderName) throws Exception {
    if (server instanceof SmtpServer) {
        throw new Exception("SMTP not applicable for reading folders");
    }

    Store store = server.createStore();
    store.connect(user, password);

    Folder newFolder = store.getFolder(folderName);
    if (! newFolder.exists()) {
        throw new Exception("No folder with name " + folderName);
    }

    newFolder.open(Folder.READ_ONLY);
    return newFolder.getMessageCount();
}
 
Example #2
Source File: EMailTestServer.java    From syndesis with Apache License 2.0 6 votes vote down vote up
public List<EMailMessageModel> getEmailsInFolder(String user, String password, String folderName) throws Exception {
    if (server instanceof SmtpServer) {
        throw new Exception("SMTP not applicable for reading folders");
    }

    Store store = server.createStore();
    store.connect(user, password);

    Folder newFolder = store.getFolder(folderName);
    if (! newFolder.exists()) {
        throw new Exception("No folder with name " + folderName);
    }

    newFolder.open(Folder.READ_ONLY);
    List<EMailMessageModel> models = new ArrayList<>();
    for (Message msg : newFolder.getMessages()) {
        models.add(createMessageModel(msg));
    }

    return models;
}
 
Example #3
Source File: GreenMail.java    From greenmail with Apache License 2.0 6 votes vote down vote up
/**
 * Create the required services according to the server setup
 *
 * @param config Service configuration
 * @return Services map
 */
protected Map<String, AbstractServer> createServices(ServerSetup[] config, Managers mgr) {
    Map<String, AbstractServer> srvc = new HashMap<>();
    for (ServerSetup setup : config) {
        if (srvc.containsKey(setup.getProtocol())) {
            throw new IllegalArgumentException("Server '" + setup.getProtocol() + "' was found at least twice in the array");
        }
        final String protocol = setup.getProtocol();
        if (protocol.startsWith(ServerSetup.PROTOCOL_SMTP)) {
            srvc.put(protocol, new SmtpServer(setup, mgr));
        } else if (protocol.startsWith(ServerSetup.PROTOCOL_POP3)) {
            srvc.put(protocol, new Pop3Server(setup, mgr));
        } else if (protocol.startsWith(ServerSetup.PROTOCOL_IMAP)) {
            srvc.put(protocol, new ImapServer(setup, mgr));
        }
    }
    return srvc;
}
 
Example #4
Source File: EMailTestServer.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public void generateFolder(String user, String password, String folderName) throws Exception {
    if (server instanceof SmtpServer) {
        throw new Exception("SMTP not applicable for generating folders");
    }

    Store store = server.createStore();
    store.connect(user, password);

    Folder newFolder = store.getFolder(folderName);
    if (! newFolder.exists()) {
        newFolder.create(Folder.HOLDS_MESSAGES);
        assertTrue(newFolder.exists());
    }

    newFolder.open(Folder.READ_WRITE);
    assertTrue(newFolder.isOpen());

    List<MimeMessage> msgs = new ArrayList<>();
    for (int i = 1; i <= 5; ++i) {
        // Use random content to avoid potential residual lingering problems
        String subject = folderName + SPACE + HYPHEN + SPACE + GreenMailUtil.random();
        String body = folderName + NEW_LINE + GreenMailUtil.random();
        GreenMailUser greenUser = greenMail.setUser(user, password);
        msgs.add(createTextMessage(greenUser.getEmail(), "Ben" + i + "@test.com", subject, body)); // Construct message
    }

    newFolder.appendMessages(msgs.toArray(new MimeMessage[0]));
    assertEquals(msgs.size(), newFolder.getMessageCount());
}
 
Example #5
Source File: ArdulinkMailOnCamelIntegrationTest.java    From Ardulink-2 with Apache License 2.0 5 votes vote down vote up
private String smtpUri(String username, String password) {
	SmtpServer smtpd = mailMock.getSmtp();
	return makeURI("smtp://" + smtpd.getBindTo() + ":" + smtpd.getPort(), newMapBuilder() //
			.put("username", username) //
			.put("password", password) //
			.put("debugMode", true) //
			.build());
}
 
Example #6
Source File: GreenMailService.java    From greenmail with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void sendMail(final String theTo,
                     final String theFrom,
                     final String theSubject,
                     final String theBody) {
    if (log.isDebugEnabled()) {
        log.debug("Sending mail, TO: <" + theTo + ">, FROM: <" + theFrom +
                ">, SUBJECT: <" + theSubject + ">, CONTENT: <" + theBody + '>');
    }

    try {
        SmtpServer smtpOrSmtpsService = (SmtpServer) (services.containsKey(ServiceProtocol.SMTP) ?
                services.get(ServiceProtocol.SMTP) : services.get(ServiceProtocol.SMTPS));
        if (null == smtpOrSmtpsService) {
            throw new IllegalStateException("No required smtp or smtps service configured!");
        }

        Session session = smtpOrSmtpsService.createSession();

        Address[] tos = new InternetAddress[]{new InternetAddress(theTo)};
        Address from = new InternetAddress(theFrom);
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setSubject(theSubject);
        mimeMessage.setFrom(from);

        mimeMessage.setText(theBody);
        Transport.send(mimeMessage, tos);
    } catch (Exception e) {
        throw new RuntimeException("Can not send mail", e);
    }
}
 
Example #7
Source File: GreenMail.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Override
public SmtpServer getSmtp() {
    return (SmtpServer) services.get(ServerSetup.PROTOCOL_SMTP);
}
 
Example #8
Source File: GreenMail.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Override
public SmtpServer getSmtps() {
    return (SmtpServer) services.get(ServerSetup.PROTOCOL_SMTPS);
}
 
Example #9
Source File: GreenMailProxy.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Override
public SmtpServer getSmtp() {
    return getGreenMail().getSmtp();
}
 
Example #10
Source File: GreenMailProxy.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Override
public SmtpServer getSmtps() {
    return getGreenMail().getSmtps();
}
 
Example #11
Source File: GreenMailOperations.java    From greenmail with Apache License 2.0 2 votes vote down vote up
/**
 * @return SMTP server operated by this GreenMail instance or null if there is none
 */
SmtpServer getSmtp();
 
Example #12
Source File: GreenMailOperations.java    From greenmail with Apache License 2.0 2 votes vote down vote up
/**
 * @return SMTP server operated by this GreenMail instance or null if there is none
 */
SmtpServer getSmtps();