Search in sources :

Example 1 with EmailStatus

use of i2p.bote.folder.Outbox.EmailStatus in project i2p.i2p-bote by i2p.

the class OutboxProcessor method sendEmail.

/**
 * Sends an {@link Email} to all recipients specified in the header.
 * @param email
 * @throws MessagingException
 * @throws DhtException
 * @throws GeneralSecurityException
 * @throws PasswordException
 * @throws IOException
 * @throws InterruptedException
 */
void sendEmail(Email email) throws MessagingException, DhtException, GeneralSecurityException, PasswordException, IOException, InterruptedException {
    EmailIdentity senderIdentity = null;
    if (!email.isAnonymous()) {
        String sender = email.getOneFromAddress();
        senderIdentity = identities.extractIdentity(sender);
        if (senderIdentity == null) {
            log.error("No identity matches the sender/from field: " + sender + " in email: " + email);
            outbox.setStatus(email, new EmailStatus(Status.NO_IDENTITY_MATCHES, sender));
        }
    }
    // send to I2P-Bote recipients
    outbox.setStatus(email, new EmailStatus(Status.SENDING));
    Address[] recipients = email.getAllRecipients();
    boolean containsExternalRecipients = false;
    for (int i = 0; i < recipients.length; i++) {
        Address recipient = recipients[i];
        if (isExternalAddress(recipient))
            containsExternalRecipients = true;
        else
            sendToOne(senderIdentity, recipient.toString(), email);
        outbox.setStatus(email, new EmailStatus(Status.SENT_TO, i + 1, recipients.length));
    }
    // send to external recipients if there are any
    if (containsExternalRecipients) {
        if (configuration.isGatewayEnabled()) {
            sendToOne(senderIdentity, configuration.getGatewayDestination(), email);
            outbox.setStatus(email, new EmailStatus(Status.EMAIL_SENT));
        } else {
            outbox.setStatus(email, new EmailStatus(Status.GATEWAY_DISABLED));
            throw new MessagingException("The email contains external addresses, but the gateway is disabled.");
        }
    }
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) EmailStatus(i2p.bote.folder.Outbox.EmailStatus)

Example 2 with EmailStatus

use of i2p.bote.folder.Outbox.EmailStatus in project i2p.i2p-bote by i2p.

the class OutboxProcessor method sendToOne.

/**
 * Sends an {@link Email} to one recipient.
 * @param senderIdentity The sender's Email Identity, or <code>null</code> for anonymous emails
 * @param recipient
 * @param email
 * @throws MessagingException
 * @throws DhtException
 * @throws GeneralSecurityException
 * @throws PasswordException
 * @throws InterruptedException
 */
private void sendToOne(EmailIdentity senderIdentity, String recipient, Email email) throws MessagingException, DhtException, GeneralSecurityException, PasswordException, InterruptedException {
    // only used for logging
    String logSuffix = null;
    try {
        logSuffix = "Recipient = '" + recipient + "' Message ID = '" + email.getMessageID() + "'";
        log.info("Sending email: " + logSuffix);
        EmailDestination recipientDest = new EmailDestination(recipient);
        EmailIdentity.IdentityConfig identityConfig;
        if (email.isAnonymous())
            identityConfig = configuration;
        else
            identityConfig = senderIdentity.getWrappedConfig(configuration);
        int hops = identityConfig.getNumStoreHops();
        long minDelay = identityConfig.getRelayMinDelay() * 60 * 1000;
        long maxDelay = identityConfig.getRelayMaxDelay() * 60 * 1000;
        int relayRedundancy = identityConfig.getRelayRedundancy();
        int maxPacketSize = getMaxEmailPacketSize(hops);
        Collection<UnencryptedEmailPacket> emailPackets = email.createEmailPackets(senderIdentity, identities, recipient, maxPacketSize);
        IndexPacket indexPacket = new IndexPacket(recipientDest);
        EmailMetadata metadata = email.getMetadata();
        for (UnencryptedEmailPacket unencryptedPacket : emailPackets) {
            EncryptedEmailPacket emailPacket = new EncryptedEmailPacket(unencryptedPacket, recipientDest);
            send(emailPacket, hops, minDelay, maxDelay, relayRedundancy);
            indexPacket.put(emailPacket);
            metadata.addPacketInfo(recipientDest, emailPacket.getDhtKey(), emailPacket.getDeleteVerificationHash());
        }
        send(indexPacket, hops, minDelay, maxDelay, relayRedundancy);
        outbox.saveMetadata(email);
    } catch (GeneralSecurityException e) {
        log.error("Invalid recipient address. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.INVALID_RECIPIENT, recipient));
        throw e;
    } catch (MessagingException e) {
        log.error("Can't create email packets. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.ERROR_CREATING_PACKETS, e.getLocalizedMessage()));
        throw e;
    } catch (DhtException e) {
        log.error("Can't store email packet on the DHT. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.ERROR_SENDING, e.getLocalizedMessage()));
        throw e;
    } catch (IOException e) {
        log.error("Can't save metadata. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.ERROR_SAVING_METADATA, e.getLocalizedMessage()));
    }
}
Also used : MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) GeneralSecurityException(java.security.GeneralSecurityException) EmailStatus(i2p.bote.folder.Outbox.EmailStatus) EmailMetadata(i2p.bote.email.EmailMetadata) IOException(java.io.IOException) IndexPacket(i2p.bote.packet.dht.IndexPacket) EmailDestination(i2p.bote.email.EmailDestination) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) DhtException(i2p.bote.network.DhtException)

Example 3 with EmailStatus

use of i2p.bote.folder.Outbox.EmailStatus in project i2p.i2p-bote by i2p.

the class BoteHelper method getEmailStatusText.

public static String getEmailStatusText(Context ctx, Email email, boolean full) {
    Resources res = ctx.getResources();
    EmailStatus emailStatus = getEmailStatus(email);
    switch(emailStatus.getStatus()) {
        case QUEUED:
            return res.getString(R.string.queued);
        case SENDING:
            return res.getString(R.string.sending);
        case SENT_TO:
            if (full)
                return res.getString(R.string.sent_to, (Integer) emailStatus.getParam1(), (Integer) emailStatus.getParam2());
            else
                return res.getString(R.string.sent_to_short, (Integer) emailStatus.getParam1(), (Integer) emailStatus.getParam2());
        case EMAIL_SENT:
            return res.getString(R.string.email_sent);
        case GATEWAY_DISABLED:
            return res.getString(R.string.gateway_disabled);
        case NO_IDENTITY_MATCHES:
            if (full)
                return res.getString(R.string.no_identity_matches, emailStatus.getParam1());
        case INVALID_RECIPIENT:
            if (full)
                return res.getString(R.string.invalid_recipient, emailStatus.getParam1());
        case ERROR_CREATING_PACKETS:
            if (full)
                return res.getString(R.string.error_creating_packets, emailStatus.getParam1());
        case ERROR_SENDING:
            if (full)
                return res.getString(R.string.error_sending, emailStatus.getParam1());
        case ERROR_SAVING_METADATA:
            if (full)
                return res.getString(R.string.error_saving_metadata, emailStatus.getParam1());
        default:
            // Short string for errors and unknown status
            return res.getString(R.string.error);
    }
}
Also used : EmailStatus(i2p.bote.folder.Outbox.EmailStatus) Resources(android.content.res.Resources)

Aggregations

EmailStatus (i2p.bote.folder.Outbox.EmailStatus)3 EmailIdentity (i2p.bote.email.EmailIdentity)2 MessagingException (javax.mail.MessagingException)2 Resources (android.content.res.Resources)1 EmailDestination (i2p.bote.email.EmailDestination)1 EmailMetadata (i2p.bote.email.EmailMetadata)1 DhtException (i2p.bote.network.DhtException)1 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)1 IndexPacket (i2p.bote.packet.dht.IndexPacket)1 UnencryptedEmailPacket (i2p.bote.packet.dht.UnencryptedEmailPacket)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Address (javax.mail.Address)1 InternetAddress (javax.mail.internet.InternetAddress)1