Search in sources :

Example 16 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class PgpMimeMessageTest method testSignedMessage.

@Test
public void testSignedMessage() throws IOException, MessagingException, PGPException {
    String messageSource = "Date: Mon, 08 Dec 2014 17:44:18 +0100\r\n" + "From: cketti <cketti@googlemail.com>\r\n" + "MIME-Version: 1.0\r\n" + "To: test@example.com\r\n" + "Subject: OpenPGP signature test\r\n" + "Content-Type: multipart/signed; micalg=pgp-sha1;\r\n" + " protocol=\"application/pgp-signature\";\r\n" + " boundary=\"24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\"\r\n" + "\r\n" + "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)\r\n" + "--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\r\n" + "Content-Type: multipart/mixed;\r\n" + " boundary=\"------------030308060900040601010501\"\r\n" + "\r\n" + "This is a multi-part message in MIME format.\r\n" + "--------------030308060900040601010501\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + "\r\n" + "Message body\r\n" + "goes here\r\n" + "\r\n" + "\r\n" + "--------------030308060900040601010501\r\n" + "Content-Type: text/plain; charset=UTF-8;\r\n" + " name=\"attachment.txt\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment;\r\n" + " filename=\"attachment.txt\"\r\n" + "\r\n" + "VGV4dCBhdHRhY2htZW50Cg==\r\n" + "--------------030308060900040601010501--\r\n" + "\r\n" + "--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\r\n" + "Content-Type: application/pgp-signature; name=\"signature.asc\"\r\n" + "Content-Description: OpenPGP digital signature\r\n" + "Content-Disposition: attachment; filename=\"signature.asc\"\r\n" + "\r\n" + "-----BEGIN PGP SIGNATURE-----\r\n" + "Version: GnuPG v1\r\n" + "\r\n" + "iQIcBAEBAgAGBQJUhdVqAAoJEO4v7zp9qOKJ8DQP/1+JE8UF7UmirnN1ZO+25hFC\r\n" + "jAfFMxRWMWXN0gGB+6ySy6ah0bCwmRwHpRBsW/tNcsmOPKb2XBf9zwF06uk/lLp4\r\n" + "ZmGXxSdQ9XJrlaHk8Sitn9Gi/1L+MNWgrsrLROAZv2jfc9wqN3FOrhN9NC1QXQvO\r\n" + "+D7sMorSr3l94majoIDrzvxEnfJVfrZWNTUaulJofOJ55GBZ3UJNob1WKjrnculL\r\n" + "IwmSERmVUoFBUfe/MBqqZH0WDJq9nt//NZFHLunj6nGsrpush1dQRcbR3zzQfXkk\r\n" + "s7zDLDa8VUv6OxcefjsVN/O7EenoWWgNg6GfW6tY2+oUsLSP2OS3JXvYsylQP4hR\r\n" + "iU1V9vvsu2Ax6bVb0+uTqw3jNiqVFy3o4mBigVUqp1EFIwBYmyNbe5wj4ACs9Avj\r\n" + "9t2reFSfXobWQFUS4s71JeMefNAHHJWZI63wNTxE6LOw01YxdJiDaPWGTOyM75MK\r\n" + "yqn7r5uIfeSv8NypGJaUv4firxKbrcZKk7Wpeh/rZuUSgoPcf3I1IzXfGKKIBHjU\r\n" + "WUMhTF5SoC5kIZyeXvHrhTM8HszcS8EoG2XcmcYArwgCUlOunFwZNqLPsfdMTRL6\r\n" + "9rcioaohEtroqoJiGAToJtIz8kqCaamnP/ASBkp9qqJizRd6fqt+tE8BsmJbuPLS\r\n" + "6lBpS8j0TqmaZMYfB9u4\r\n" + "=QvET\r\n" + "-----END PGP SIGNATURE-----\r\n" + "\r\n" + "--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM--\r\n";
    BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
    InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
    MimeMessage message;
    try {
        message = MimeMessage.parseMimeMessage(messageInputStream, true);
    } finally {
        messageInputStream.close();
    }
    Multipart multipartSigned = (Multipart) message.getBody();
    BodyPart signedPart = multipartSigned.getBodyPart(0);
    ByteArrayOutputStream signedPartOutputStream = new ByteArrayOutputStream();
    signedPart.writeTo(signedPartOutputStream);
    byte[] signedData = signedPartOutputStream.toByteArray();
    Body signatureBody = multipartSigned.getBodyPart(1).getBody();
    ByteArrayOutputStream signatureBodyOutputStream = new ByteArrayOutputStream();
    signatureBody.writeTo(signatureBodyOutputStream);
    byte[] signatureData = signatureBodyOutputStream.toByteArray();
    assertTrue(verifySignature(signedData, signatureData));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) Test(org.junit.Test)

Example 17 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class ReconstructMessageTest method testMessage.

@Test
public void testMessage() throws IOException, MessagingException {
    String messageSource = "From: from@example.com\r\n" + "To: to@example.com\r\n" + "Subject: Test Message \r\n" + "Date: Thu, 13 Nov 2014 17:09:38 +0100\r\n" + "Content-Type: multipart/mixed;\r\n" + " boundary=\"----Boundary\"\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "MIME-Version: 1.0\r\n" + "\r\n" + "This is a multipart MIME message.\r\n" + "------Boundary\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "Testing.\r\n" + "This is a text body with some greek characters.\r\n" + "αβγδεζηθ\r\n" + "End of test.\r\n" + "\r\n" + "------Boundary\r\n" + "Content-Type: text/plain\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + "VGhpcyBpcyBhIHRl\r\n" + "c3QgbWVzc2FnZQ==\r\n" + "\r\n" + "------Boundary--\r\n" + "Hi, I'm the epilogue";
    BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
    InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
    MimeMessage message;
    try {
        message = MimeMessage.parseMimeMessage(messageInputStream, true);
    } finally {
        messageInputStream.close();
    }
    ByteArrayOutputStream messageOutputStream = new ByteArrayOutputStream();
    try {
        message.writeTo(messageOutputStream);
    } finally {
        messageOutputStream.close();
    }
    String reconstructedMessage = new String(messageOutputStream.toByteArray());
    assertEquals(messageSource, reconstructedMessage);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 18 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class EmailProviderTest method buildMessages.

private void buildMessages() throws MessagingException {
    message = new MimeMessage();
    message.setSubject("Test Subject");
    message.setSentDate(new GregorianCalendar(2016, 1, 2).getTime(), false);
    message.setMessageId("<uid001@email.com>");
    laterMessage = new MimeMessage();
    laterMessage.setSubject("Test Subject2");
    laterMessage.setSentDate(new GregorianCalendar(2016, 1, 3).getTime(), false);
    reply = new MimeMessage();
    reply.setSubject("Re: Test Subject");
    reply.setSentDate(new GregorianCalendar(2016, 1, 3).getTime(), false);
    reply.setMessageId("<uid002@email.com>");
    reply.setInReplyTo("<uid001@email.com>");
    replyAtSameTime = new MimeMessage();
    replyAtSameTime.setSubject("Re: Test Subject");
    replyAtSameTime.setSentDate(new GregorianCalendar(2016, 1, 2).getTime(), false);
    replyAtSameTime.setMessageId("<uid002@email.com>");
    replyAtSameTime.setInReplyTo("<uid001@email.com>");
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) GregorianCalendar(java.util.GregorianCalendar)

Example 19 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class K9 method onCreate.

@Override
public void onCreate() {
    if (K9.DEVELOPER_MODE) {
        StrictMode.enableDefaults();
    }
    PRNGFixes.apply();
    super.onCreate();
    app = this;
    Globals.setContext(this);
    K9MailLib.setDebugStatus(new K9MailLib.DebugStatus() {

        @Override
        public boolean enabled() {
            return DEBUG;
        }

        @Override
        public boolean debugSensitive() {
            return DEBUG_SENSITIVE;
        }
    });
    checkCachedDatabaseVersion();
    Preferences prefs = Preferences.getPreferences(this);
    loadPrefs(prefs);
    /*
         * We have to give MimeMessage a temp directory because File.createTempFile(String, String)
         * doesn't work in Android and MimeMessage does not have access to a Context.
         */
    BinaryTempFileBody.setTempDirectory(getCacheDir());
    LocalKeyStore.setKeyStoreLocation(getDir("KeyStore", MODE_PRIVATE).toString());
    /*
         * Enable background sync of messages
         */
    setServicesEnabled(this);
    registerReceivers();
    MessagingController.getInstance(this).addListener(new SimpleMessagingListener() {

        private void broadcastIntent(String action, Account account, String folder, Message message) {
            Uri uri = Uri.parse("email://messages/" + account.getAccountNumber() + "/" + Uri.encode(folder) + "/" + Uri.encode(message.getUid()));
            Intent intent = new Intent(action, uri);
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_ACCOUNT, account.getDescription());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FOLDER, folder);
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_SENT_DATE, message.getSentDate());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM, Address.toString(message.getFrom()));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_TO, Address.toString(message.getRecipients(Message.RecipientType.TO)));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_CC, Address.toString(message.getRecipients(Message.RecipientType.CC)));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_BCC, Address.toString(message.getRecipients(Message.RecipientType.BCC)));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_SUBJECT, message.getSubject());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM_SELF, account.isAnIdentity(message.getFrom()));
            K9.this.sendBroadcast(intent);
            Timber.d("Broadcasted: action=%s account=%s folder=%s message uid=%s", action, account.getDescription(), folder, message.getUid());
        }

        private void updateUnreadWidget() {
            try {
                UnreadWidgetProvider.updateUnreadCount(K9.this);
            } catch (Exception e) {
                Timber.e(e, "Error while updating unread widget(s)");
            }
        }

        private void updateMailListWidget() {
            try {
                MessageListWidgetProvider.triggerMessageListWidgetUpdate(K9.this);
            } catch (RuntimeException e) {
                if (BuildConfig.DEBUG) {
                    throw e;
                } else {
                    Timber.e(e, "Error while updating message list widget");
                }
            }
        }

        @Override
        public void synchronizeMailboxRemovedMessage(Account account, String folder, Message message) {
            broadcastIntent(K9.Intents.EmailReceived.ACTION_EMAIL_DELETED, account, folder, message);
            updateUnreadWidget();
            updateMailListWidget();
        }

        @Override
        public void messageDeleted(Account account, String folder, Message message) {
            broadcastIntent(K9.Intents.EmailReceived.ACTION_EMAIL_DELETED, account, folder, message);
            updateUnreadWidget();
            updateMailListWidget();
        }

        @Override
        public void synchronizeMailboxNewMessage(Account account, String folder, Message message) {
            broadcastIntent(K9.Intents.EmailReceived.ACTION_EMAIL_RECEIVED, account, folder, message);
            updateUnreadWidget();
            updateMailListWidget();
        }

        @Override
        public void folderStatusChanged(Account account, String folderName, int unreadMessageCount) {
            updateUnreadWidget();
            updateMailListWidget();
            // let observers know a change occurred
            Intent intent = new Intent(K9.Intents.EmailReceived.ACTION_REFRESH_OBSERVER, null);
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_ACCOUNT, account.getDescription());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FOLDER, folderName);
            K9.this.sendBroadcast(intent);
        }
    });
    notifyObservers();
}
Also used : Message(com.fsck.k9.mail.Message) Intent(android.content.Intent) Uri(android.net.Uri) K9MailLib(com.fsck.k9.mail.K9MailLib) SimpleMessagingListener(com.fsck.k9.controller.SimpleMessagingListener) SharedPreferences(android.content.SharedPreferences)

Example 20 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class MessageCompose method onMessageBuildSuccess.

@Override
public void onMessageBuildSuccess(MimeMessage message, boolean isDraft) {
    if (isDraft) {
        changesMadeSinceLastSave = false;
        currentMessageBuilder = null;
        if (action == Action.EDIT_DRAFT && relatedMessageReference != null) {
            message.setUid(relatedMessageReference.getUid());
        }
        // TODO more appropriate logic here? not sure
        boolean saveRemotely = !recipientPresenter.getCurrentCryptoStatus().shouldUsePgpMessageBuilder();
        new SaveMessageTask(getApplicationContext(), account, contacts, internalMessageHandler, message, draftId, saveRemotely).execute();
        if (finishAfterDraftSaved) {
            finish();
        } else {
            setProgressBarIndeterminateVisibility(false);
        }
    } else {
        currentMessageBuilder = null;
        new SendMessageTask(getApplicationContext(), account, contacts, message, draftId != INVALID_DRAFT_ID ? draftId : null, relatedMessageReference).execute();
        finish();
    }
}
Also used : SaveMessageTask(com.fsck.k9.activity.compose.SaveMessageTask)

Aggregations

MimeMessage (com.fsck.k9.mail.internet.MimeMessage)44 Test (org.junit.Test)35 TextBody (com.fsck.k9.mail.internet.TextBody)16 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)14 Part (com.fsck.k9.mail.Part)11 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 ArrayList (java.util.ArrayList)9 BodyPart (com.fsck.k9.mail.BodyPart)8 Message (com.fsck.k9.mail.Message)7 Viewable (com.fsck.k9.mail.internet.Viewable)7 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)7 Intent (android.content.Intent)6 OutputStream (java.io.OutputStream)6 PendingIntent (android.app.PendingIntent)5 Callback (com.fsck.k9.message.MessageBuilder.Callback)5 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)5 ComposeCryptoStatus (com.fsck.k9.activity.compose.ComposeCryptoStatus)4 Address (com.fsck.k9.mail.Address)4 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)4