Search in sources :

Example 1 with Attachment

use of i2p.bote.email.Attachment in project i2p.i2p-bote by i2p.

the class NewEmailFragment method sendEmail.

private boolean sendEmail() {
    Email email = new Email(I2PBote.getInstance().getConfiguration().getIncludeSentTime());
    try {
        // Set sender
        EmailIdentity sender = (EmailIdentity) mSpinner.getSelectedItem();
        InternetAddress ia = new InternetAddress(sender == null ? "Anonymous" : BoteHelper.getNameAndDestination(sender.getKey()));
        email.setFrom(ia);
        // We must continue to set "Sender:" even with only one mailbox
        // in "From:", which is against RFC 2822 but required for older
        // Bote versions to see a sender (and validate the signature).
        email.setSender(ia);
        for (Object obj : mTo.getObjects()) {
            Person person = (Person) obj;
            email.addRecipient(Message.RecipientType.TO, new InternetAddress(person.getAddress(), person.getName()));
        }
        if (mMoreVisible) {
            for (Object obj : mCc.getObjects()) {
                Person person = (Person) obj;
                email.addRecipient(Message.RecipientType.CC, new InternetAddress(person.getAddress(), person.getName()));
            }
            for (Object obj : mBcc.getObjects()) {
                Person person = (Person) obj;
                email.addRecipient(Message.RecipientType.BCC, new InternetAddress(person.getAddress(), person.getName()));
            }
        }
        // Check that we have someone to send to
        Address[] rcpts = email.getAllRecipients();
        if (rcpts == null || rcpts.length == 0) {
            // No recipients
            mTo.setError(getActivity().getString(R.string.add_one_recipient));
            mTo.requestFocus();
            return false;
        } else {
            mTo.setError(null);
        }
        email.setSubject(mSubject.getText().toString(), "UTF-8");
        // Extract the attachments
        List<Attachment> attachments = new ArrayList<Attachment>();
        for (int i = 0; i < mAttachments.getChildCount(); i++) {
            View v = mAttachments.getChildAt(i);
            // Warning views don't have tags set
            if (v.getTag() != null)
                attachments.add((Attachment) v.getTag());
        }
        // Set the text and add attachments
        email.setContent(mContent.getText().toString(), attachments);
        // Cache the fact that we sent this email
        BoteHelper.setEmailSent(email, true);
        // Send the email
        I2PBote.getInstance().sendEmail(email);
        // Clean up attachments
        for (Attachment attachment : attachments) {
            if (!attachment.clean())
                Log.e(Constants.ANDROID_LOG_TAG, "Can't clean up attachment: <" + attachment + ">");
        }
        return true;
    } catch (PasswordException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Email(i2p.bote.email.Email) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Attachment(i2p.bote.email.Attachment) ContentAttachment(i2p.bote.android.util.ContentAttachment) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ContactsCompletionView(i2p.bote.android.widget.ContactsCompletionView) SuppressLint(android.annotation.SuppressLint) PasswordException(i2p.bote.fileencryption.PasswordException) AddressException(javax.mail.internet.AddressException) Person(i2p.bote.android.util.Person)

Example 2 with Attachment

use of i2p.bote.email.Attachment in project i2p.i2p-bote by i2p.

the class SendEmailTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    JspWriter out = pageContext.getOut();
    Email email = new Email(includeSentTime);
    String statusMessage;
    if (recipients.isEmpty())
        statusMessage = _t("Error: Please add at least one recipient.");
    else
        try {
            // set addresses
            InternetAddress ia = new InternetAddress(senderAddress);
            email.setFrom(ia);
            // We must continue to set "Sender:" even with only one mailbox
            // in "From:", which is against RFC 2822 but required for older
            // Bote versions to see a sender (and validate the signature).
            email.setSender(ia);
            email.setSubject(subject, "UTF-8");
            for (Recipient recipient : recipients) email.addRecipient(recipient.type, recipient.address);
            // TODO: Comment out until we determine if this is necessary
            // email.fixAddresses();
            // set the text and add attachments
            email.setContent(message, attachments);
            // send the email
            I2PBote.getInstance().sendEmail(email);
            // delete attachment temp files
            for (Attachment attachment : attachments) {
                if (!attachment.clean())
                    log.error("Can't clean up attachment: <" + attachment + ">");
            }
            statusMessage = _t("The email has been queued for sending.");
        } catch (PasswordException e) {
            throw new JspException(e);
        } catch (NoIdentityForSenderException e) {
            statusMessage = _t("Error sending email: {0}", _t("No identity matches the sender/from field: {0}", e.getSender()));
            log.error("Error sending email", e);
        } catch (AddressException e) {
            statusMessage = _t("Error sending email: {0}", _t("Address doesn't contain an Email Destination or an external address: {0}", e.getRef()));
            log.error("Error sending email", e);
        } catch (Exception e) {
            statusMessage = _t("Error sending email: {0}", e.getLocalizedMessage());
            log.error("Error sending email", e);
        }
    try {
        out.println(statusMessage);
    } catch (IOException e) {
        log.error("Can't write output to HTML page", e);
    }
    return EVAL_PAGE;
}
Also used : PasswordException(i2p.bote.fileencryption.PasswordException) JspException(javax.servlet.jsp.JspException) InternetAddress(javax.mail.internet.InternetAddress) Email(i2p.bote.email.Email) NoIdentityForSenderException(i2p.bote.email.NoIdentityForSenderException) AddressException(javax.mail.internet.AddressException) Attachment(i2p.bote.email.Attachment) FileAttachment(i2p.bote.email.FileAttachment) IOException(java.io.IOException) JspWriter(javax.servlet.jsp.JspWriter) AddressException(javax.mail.internet.AddressException) PasswordException(i2p.bote.fileencryption.PasswordException) JspException(javax.servlet.jsp.JspException) IOException(java.io.IOException) NoIdentityForSenderException(i2p.bote.email.NoIdentityForSenderException)

Example 3 with Attachment

use of i2p.bote.email.Attachment in project i2p.i2p-bote by i2p.

the class AttachmentProviderTests method createEmailWithAttachment.

private Uri createEmailWithAttachment(Attachment attachment) throws Exception {
    List<Attachment> attachments = new ArrayList<Attachment>();
    attachments.add(attachment);
    Email email = new Email(false);
    email.setContent("", attachments);
    I2PBote.getInstance().getInbox().add(email);
    return AttachmentProvider.getUriForAttachment(I2PBote.getInstance().getInbox().getName(), email.getMessageID(), 1);
}
Also used : Email(i2p.bote.email.Email) ArrayList(java.util.ArrayList) Attachment(i2p.bote.email.Attachment) ContentAttachment(i2p.bote.android.util.ContentAttachment)

Aggregations

Attachment (i2p.bote.email.Attachment)3 Email (i2p.bote.email.Email)3 ContentAttachment (i2p.bote.android.util.ContentAttachment)2 PasswordException (i2p.bote.fileencryption.PasswordException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AddressException (javax.mail.internet.AddressException)2 InternetAddress (javax.mail.internet.InternetAddress)2 SuppressLint (android.annotation.SuppressLint)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 Person (i2p.bote.android.util.Person)1 ContactsCompletionView (i2p.bote.android.widget.ContactsCompletionView)1 EmailIdentity (i2p.bote.email.EmailIdentity)1 FileAttachment (i2p.bote.email.FileAttachment)1 NoIdentityForSenderException (i2p.bote.email.NoIdentityForSenderException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Address (javax.mail.Address)1 MessagingException (javax.mail.MessagingException)1