| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Protocol |
|
| 1.0;1 | ||||
| Protocol$SMTP |
|
| 1.0;1 | ||||
| Protocol$SMTPS |
|
| 1.0;1 |
| 1 | /** | |
| 2 | * Copyright (c) 2014-2017, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.email; | |
| 31 | ||
| 32 | import com.google.common.collect.ImmutableMap; | |
| 33 | import com.jcabi.aspects.Immutable; | |
| 34 | import java.util.Map; | |
| 35 | import javax.net.ssl.SSLSocketFactory; | |
| 36 | ||
| 37 | /** | |
| 38 | * Mail network protocol. | |
| 39 | * | |
| 40 | * <p>This is how you're supposed to use it: | |
| 41 | * | |
| 42 | * <pre> Postman postman = new Postman.Default( | |
| 43 | * new SMTP( | |
| 44 | * new Token("user", "password").access( | |
| 45 | * new Protocol.SMTP("bind", "port") | |
| 46 | * ) | |
| 47 | * ) | |
| 48 | * ); | |
| 49 | * </pre> | |
| 50 | * For <b>SMTPS</b> use the {@link com.jcabi.email.wire.SMTPS} wire and | |
| 51 | * {@link Protocol.SMTPS} respectively. SMTPS protocol should work only with | |
| 52 | * the SMTPS wire because the wire uses the "smtps" transport to make the | |
| 53 | * connection to the mail server: | |
| 54 | * <pre> | |
| 55 | * final Transport transport = this.session.getTransport("smtps"); | |
| 56 | * transport.connect(); | |
| 57 | * </pre> | |
| 58 | * | |
| 59 | * @author Piotr Kotlicki (piotr.kotlicki@gmail.com) | |
| 60 | * @version $Id: cf50af999224ef6f1607619c650ea6d122dc9bd3 $ | |
| 61 | * @since 1.0 | |
| 62 | */ | |
| 63 | @Immutable | |
| 64 | public interface Protocol { | |
| 65 | ||
| 66 | /** | |
| 67 | * Guarantee of access for protocol. | |
| 68 | * @return Entry parameters. | |
| 69 | */ | |
| 70 | Map<String, String> entries(); | |
| 71 | ||
| 72 | /** | |
| 73 | * SNMP protocol. | |
| 74 | */ | |
| 75 | final class SMTP implements Protocol { | |
| 76 | /** | |
| 77 | * SMTP host. | |
| 78 | */ | |
| 79 | private final transient String host; | |
| 80 | ||
| 81 | /** | |
| 82 | * SMTP port. | |
| 83 | */ | |
| 84 | private final transient int port; | |
| 85 | ||
| 86 | /** | |
| 87 | * Public ctor. | |
| 88 | * @param hst SMTP Host | |
| 89 | * @param prt SMTP Port | |
| 90 | */ | |
| 91 | 1 | public SMTP(final String hst, final int prt) { |
| 92 | 1 | this.host = hst; |
| 93 | 1 | this.port = prt; |
| 94 | 1 | } |
| 95 | ||
| 96 | @Override | |
| 97 | public Map<String, String> entries() { | |
| 98 | 1 | return new ImmutableMap.Builder<String, String>() |
| 99 | .put("mail.smtp.auth", Boolean.TRUE.toString()) | |
| 100 | .put("mail.smtp.host", this.host) | |
| 101 | .put("mail.smtp.port", Integer.toString(this.port)) | |
| 102 | .build(); | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * SMTPS protocol. | |
| 108 | */ | |
| 109 | final class SMTPS implements Protocol { | |
| 110 | /** | |
| 111 | * SMTPS host. | |
| 112 | */ | |
| 113 | private final transient String host; | |
| 114 | ||
| 115 | /** | |
| 116 | * SMTPS port. | |
| 117 | */ | |
| 118 | private final transient int port; | |
| 119 | ||
| 120 | /** | |
| 121 | * Public ctor. | |
| 122 | * @param hst SMTPS Host | |
| 123 | * @param prt SMTPS Port | |
| 124 | */ | |
| 125 | 1 | public SMTPS(final String hst, final int prt) { |
| 126 | 1 | this.host = hst; |
| 127 | 1 | this.port = prt; |
| 128 | 1 | } |
| 129 | ||
| 130 | @Override | |
| 131 | public Map<String, String> entries() { | |
| 132 | 1 | return new ImmutableMap.Builder<String, String>() |
| 133 | .put("mail.smtps.auth", Boolean.TRUE.toString()) | |
| 134 | .put("mail.smtps.host", this.host) | |
| 135 | .put("mail.smtps.port", Integer.toString(this.port)) | |
| 136 | .put( | |
| 137 | "mail.smtp.ssl.checkserveridentity", | |
| 138 | Boolean.TRUE.toString() | |
| 139 | ).put( | |
| 140 | "mail.smtp.socketFactory.class", | |
| 141 | SSLSocketFactory.class.getName() | |
| 142 | ).put( | |
| 143 | "mail.smtp.socketFactory.port", | |
| 144 | Integer.toString(this.port) | |
| 145 | ).put("mail.smtp.socketFactory.fallback", "false").build(); | |
| 146 | } | |
| 147 | } | |
| 148 | } |