Unable to read BCC field of sent mails in java - java

I am facing one problem while extracting BCC address from incoming mail.
Here is the sample code that is used.
public EmailVO dumpEnvelope(Message m) throws Exception {
EmailVO emailVO = new EmailVO();
if ((addresses = m.getRecipients(Message.RecipientType.BCC)) != null) {
emailVO.setBcc(this.getAddresses(addresses, "BCC"));
}
}
I am getting null value in BCC.
While debugging I found BCC recipient's name in header field but I am not able to access that header.
Is this code issue or there is some specific setting while sending mail like not to include BCC fields?

The whole point of Bcc is that it's a blind carbon copy - the recipients don't get to see who was copied. You won't see Bcc fields in messages you receive. (Sometimes a Bcc'ed recipient will see the Bcc header in the messages they receive, but the other recipients will get a copy of the message without the Bcc. But I don't think many mailers do that anymore because it requires sending two different versions of the message.)

You can check your Message object which contains all the details about the mails.
As the BCC is the part of mail but also it will be hidden, but as per my knowledge you can retrieve the information from your mail headers.
Address[] addresses = m.getHeader("Your Header Name HERE");
This will give you all the details regarding your particular header tag in mails.
for e.g.
Address[] addresses = m.getHeader("Delivered-To");
This tag will give you all the information about the recipients of the mail, which will also include BCC.
you can also add your custom headers for mail.

addresses = m.getRecipients(Message.RecipientType.BCC);
returns an array of addresses. You can check the content in a for loop:
Address[] addresses = m.getRecipients(Message.RecipientType.BCC);
for(Address address : addresses){
System.out.println(address);
}

Related

how to set destination to SendRawEmailRequest [duplicate]

How to add the list of Cc and Bcc recipients in sendrawemail (java). I'm just adding all the recipients to one list and sending the mail. There is no separate method to set Cc and Bcc for SendRawEmailRequest.
Is there any way to set object of Destination type?
List<String> receipients = new ArrayList<String>();
receipients.addAll(mailToRecipients);
receipients.addAll(mailCcRecipients);
receipients.addAll(mailBccRecipients);
SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage).withDestinations(receipients);
Regarding SendRawEmail, you should be able to differentiate To, Cc, and Bcc destinations by setting them in your raw message headers. If you don't explicitly specify destinations in the request object, the headers will be checked instead. If you do, the headers won't be checked.
Here's a great example regarding this problem that JustinC#AWS shared on the AWS forums:
Destinations: (empty)
To: A#example.com
Cc: B#example.com
Bcc: C#example.com
The above message will be sent to all three of A#, B#, C#example.com.
In contrast, if you send the following input:
Destinations: A#example.com
To: A#example.com
Cc: B#example.com
Bcc: C#example.com
That message will be delivered only to A#example.com.

sending an email from different address

I'm writing a program that has auto response in it. i.e. for example there is an user ad his email address would be user#domain.com, when ever someone writes an email to this address, there should be an auto response sent (Which I'm able to do it). but this should be sent from autoReply#domain.com(this is what I'm unable to know on how to do this). autoReply#domain.com is a shared mail box.
I'm using ews in my program and the below block of code is responsible to send the auto response.
private void replyToEmailWithURLs(ItemId itemId, List matchedKeyWords, String fromAddress) throws Exception {
EmailMessage message = EmailMessage.bind(service, itemId, new PropertySet(BasePropertySet.IdOnly));
ResponseMessage responseMessage = message.createReply(false);
message.getReplyTo().add(new EmailAddress("autoReply#domain.com"));
// Add autoReply in CC
responseMessage.getCcRecipients().add("autoReply#domain.com");
String responseUrls = getTheResponseUrlsFromJsonFile(matchedKeyWords, fromAddress);
responseMessage.setBodyPrefix(new MessageBody(BodyType.HTML, responseUrls));
responseMessage.sendAndSaveCopy();
message.setIsRead(true);
}
Here I tried to add a replyTo using message.getReplyTo().add(new EmailAddress("autoReply#domain.com"));, even this doesn't work.
I've seen a post here set reply-to address in outgoing email EWS , I was confused on the way it is working(basically given in c#), please let me know how can I get this done in Java.
Thanks
I'm not familiar with EWS, but apparently the standard JavaMail API supports setting From or the sender address.

Sending mail by setting recipient address in setHeader() method - java mail

Is there any way to add Recipient address using setHeader() like
message.setHeader("To:","mail#domain.com");
I tried the above one and it is not working. Please correct me if I am wrong.
I need an alternative for the following code:
InternetAddress mail_to = new InternetAddress("mail_id#domain.com","Name_of_recipient");
message.addRecipient(Message.RecipientType.TO, mail_to);
My requirement is to skip RFC822 email format check.
The header name is "To", without the colon.
But you're not going to get very far with an incorrectly formatted address. Even if JavaMail doesn't detect the error, the server most likely will.

sending email using "bcc" without "to" in java application

I have read all the reference in stackoverflow. However, nothing matches in our goal. How can i use bcc in sendmail method in java?
According to the RFC for SMTP, RFC 2821 (link), it is not possible to send an email message without a To: header. (You cannot send an RCPT command without it, see section 3.3.)
As Dietrich mentions, that's not possible with the RFC. If the primary goal is to send to the bcc target email addresses, you could provide a dummy to email address (such as your own email address or a reply-to email), which would fulfill that technicality while still allowing you to send the email to the desired bcc targets.
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress("joe#example.com"));
By default, JavaMail collects all the recipients specified on the Message object, including Bcc recipients, and uses them in the RCPT command to the SMTP server. The Bcc recipients won't show up in the message headers, however (which is the whole point of Bcc).

How can I send two emails via javamail such that outlook will put them in the same conversation?

At some point, I've sent an email and it is processed and received by an exchange server and then viewed by an outlook client. At a later point, I send another email where it is a reply/related to the 1st email. I want outlook to know that they are related and group them together when I group emails by conversation.
A couple of caveats:
I won't have access to the 1st email's message object to reply off of.
I will have the subject, messageID, and body of the first email when composing the 2nd email.
I've tried adding the "in-reply-to" and "references" header fields in the 2nd email, but outlook will not conversate them. Thunderbird and Gmail will thread them appropriately, though.
When grouping messages into a Conversation, Outlook will most likely not use the header fields but rather the subject line. It will ignore words like Fw: and Re:.
Try prepend a Re: to the first message subject and use this as second message subject.
Ref: http://lifehacker.com/157042/make-outlook-thread-conversations-like-gmail

Categories