@@ -103,7 +103,7 @@ public void runSMTPAction(AccountDao accountDao, Session session, Store store, S
103103
104104 updateContactsLastUseDateTime (context );
105105
106- MimeMessage mimeMessage = createMimeMessage (session , context , pgpCacheDirectory );
106+ MimeMessage mimeMessage = createMimeMessage (session , context , accountDao , pgpCacheDirectory );
107107
108108 Transport transport = prepareTransportForSmtp (syncListener .getContext (), session , accountDao );
109109 transport .sendMessage (mimeMessage , mimeMessage .getAllRecipients ());
@@ -155,16 +155,16 @@ private void saveCopyOfSentMessage(AccountDao accountDao, Store store, Context c
155155 *
156156 * @param session Will be used to create {@link MimeMessage}
157157 * @param context Interface to global information about an application environment.
158- * @param pgpCacheDirectory The cache directory which contains temp files .
159- * @return {@link MimeMessage}
158+ * @param accountDao The {@link AccountDao} which contains information about account .
159+ * @param pgpCacheDirectory The cache directory which contains temp files. @ return {@link MimeMessage}
160160 * @throws IOException
161161 * @throws MessagingException
162162 */
163163 @ NonNull
164- private MimeMessage createMimeMessage (Session session , Context context , File pgpCacheDirectory )
165- throws IOException , MessagingException {
164+ private MimeMessage createMimeMessage (Session session , Context context , AccountDao accountDao ,
165+ File pgpCacheDirectory ) throws IOException , MessagingException {
166166 Js js = new Js (context , new SecurityStorageConnector (context ));
167- String [] pubKeys = getPubKeys (js , context );
167+ String [] pubKeys = getPubKeys (context , js , accountDao );
168168
169169 String rawMessage = generateRawMessageWithoutAttachments (js , pubKeys );
170170
@@ -288,42 +288,59 @@ private void updateContactsLastUseDateTime(Context context) {
288288 /**
289289 * Get public keys for recipients + keys of the sender;
290290 *
291- * @param js - {@link Js} util class.
292- * @param context - Interface to global information about an application environment.
291+ * @param context Interface to global information about an application environment.
292+ * @param accountDao The {@link AccountDao} which contains information about account.
293+ * @param js - {@link Js} util class.
293294 * @return <tt>String[]</tt> An array of public keys.
294295 */
295- private String [] getPubKeys (Js js , Context context ) {
296+ private String [] getPubKeys (Context context , Js js , AccountDao accountDao ) {
296297 ArrayList <String > publicKeys = new ArrayList <>();
297298 for (PgpContact pgpContact : outgoingMessageInfo .getToPgpContacts ()) {
298299 if (!TextUtils .isEmpty (pgpContact .getPubkey ())) {
299300 publicKeys .add (pgpContact .getPubkey ());
300301 }
301302 }
302303
303- publicKeys .addAll ( generateOwnPublicKeys ( js , context ));
304+ publicKeys .add ( getAccountPublicKey ( context , js , accountDao ));
304305
305306 return publicKeys .toArray (new String [0 ]);
306307 }
307308
308309 /**
309- * Get public keys of the sender;
310+ * Get a public key of the sender;
310311 *
311- * @param js - {@link Js} util class.
312- * @param context - Interface to global information about an application environment.
313- * @return <tt>String[]</tt> An array of the sender public keys.
312+ * @param context Interface to global information about an application environment.
313+ * @param js {@link Js} util class.
314+ * @param accountDao The {@link AccountDao} which contains information about account.
315+ * @return <tt>String</tt> The sender public key.
314316 */
315- private ArrayList < String > generateOwnPublicKeys ( Js js , Context context ) {
316- ArrayList < String > publicKeys = new ArrayList <>( );
317+ private String getAccountPublicKey ( Context context , Js js , AccountDao accountDao ) {
318+ PgpContact pgpContact = new ContactsDaoSource (). getPgpContact ( context , accountDao . getEmail () );
317319
318- SecurityStorageConnector securityStorageConnector = new SecurityStorageConnector (context );
319- PgpKeyInfo [] pgpKeyInfoArray = securityStorageConnector .getAllPgpPrivateKeys ();
320+ if (pgpContact != null && !TextUtils .isEmpty (pgpContact .getPubkey ())) {
321+ return pgpContact .getPubkey ();
322+ }
320323
324+ PgpKeyInfo [] pgpKeyInfoArray = new SecurityStorageConnector (context ).getAllPgpPrivateKeys ();
321325 for (PgpKeyInfo pgpKeyInfo : pgpKeyInfoArray ) {
322326 PgpKey pgpKey = js .crypto_key_read (pgpKeyInfo .getArmored ());
323- publicKeys .add (pgpKey .toPublic ().armor ());
327+ if (pgpKey != null ) {
328+ PgpKey publicKey = pgpKey .toPublic ();
329+ if (publicKey != null ) {
330+ PgpContact primaryUserId = pgpKey .getPrimaryUserId ();
331+ if (primaryUserId != null ) {
332+ if (!TextUtils .isEmpty (publicKey .armor ())) {
333+ primaryUserId .setPubkey (publicKey .armor ());
334+ new ContactsDaoSource ().addRow (context , primaryUserId );
335+ return primaryUserId .getPubkey ();
336+ }
337+ break ;
338+ }
339+ }
340+ }
324341 }
325342
326- return publicKeys ;
343+ throw new IllegalArgumentException ( "The sender doesn't have a public key" ) ;
327344 }
328345
329346 /**
0 commit comments