Showing un known sender

When i am going to get messages to mbox file and get mail Messages and append it on the imap server like gmail
i got un known sender in my sender and recipient like to , from
i am sharing you a code sneptPreformatted text

try {
			MailAddress from = new MailAddress(message.getFrom().getAddress().toString());
			message.setFrom(from);
			MailAddressCollection meilcol = new MailAddressCollection();
			if (message.getTo() == null) {
				MailAddress address = new MailAddress(message.getTo().get_Item(0).getAddress());
				meilcol.addMailAddress(address);
			}
			message.setTo(meilcol);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			MailAddressCollection bcccoll = new MailAddressCollection();
			if (message.getBcc() == null) {
				bcccoll.add(message.getBcc().get_Item(0).getAddress());
			}
			message.setBcc(bcccoll);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		try {
			MailAddressCollection bcccoll = new MailAddressCollection();
			if (message.getCC() == null) {
				bcccoll.add(message.getCC().get_Item(0).getAddress());
			}
			message.setCC(bcccoll);
		} catch (Exception e) {
			// e.printStackTrace();
		}

Hello @AnkitDev ,

Thank you for writing to Aspose support. We’ll look into your case and get back to you with a solution or any follow-up questions we may have.

@AnkitDev,

Thank you for sharing the code snippet.
We see you’re trying to retrieve messages from an MBOX file and then upload them to an IMAP server like Gmail, but you’re encountering an issue where the sender and recipient fields (From, To, etc.) show as “unknown”.

Looking at your code, there are a few issues:

  • You’re using if (message.getTo() == null) (and similar for Bcc and Cc) to check if the collection exists, but then you immediately try to access the first item in the collection using get_Item(0). If the collection is null, this will cause a NullPointerException.
    Instead, you should check if the collection is not null and has items before accessing its elements.
  • You’re overwriting the entire To, Cc, and Bcc fields with new collections without preserving the original addresses.