Java源码示例:org.apache.felix.scr.annotations.Properties

示例1
/**
 * Send an email.
 *
 * @param recipient The recipient of the email
 * @param subject The subject of the email.
 * @param body The body of the email.
 * @return true if the email was sent successfully.
 */
public boolean sendMail(final String recipient, final String subject, final String body) {
    boolean result = false;

    // Create a Properties object to contain connection configuration information.
    java.util.Properties props = System.getProperties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.port", getPort());

    // Set properties indicating that we want to use STARTTLS to encrypt the connection.
    // The SMTP session will begin on an unencrypted connection, and then the client
    // will issue a STARTTLS command to upgrade to an encrypted connection.
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");

    // Create a Session object to represent a mail session with the specified properties.
    Session session = Session.getDefaultInstance(props);

    // Create a message with the specified information.
    MimeMessage msg = new MimeMessage(session);

    Transport transport = null;

    // Send the message.
    try {
        msg.setFrom(new InternetAddress(getSender()));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
        msg.setSubject(subject);
        msg.setContent(body, "text/plain");

        // Create a transport.
        transport = session.getTransport();

        // Connect to email server using the SMTP username and password you specified above.
        transport.connect(getHost(), getSmtpUsername(), getUnobfuscatedSmtpPassword());

        // Send the email.
        transport.sendMessage(msg, msg.getAllRecipients());

        result = true;
    } catch (Exception ex) {
        LOGGER.error("The email was not sent.", ex);
    } finally {
        // Close and terminate the connection.
        try {
            transport.close();
        } catch (MessagingException e) {
            LOGGER.error("Could not close transport", e);
        }
    }

    return result;
}