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.
Related
Background:
There are some invalid email adress in our system. I want to find all these invalid addresses. Some of these address are outlook address and some are instant message (IM) address.
Issue:
I have meet an issue when use SMTP to validate email address.
SMTP works when validate outlook address like ab1234#outlook.com.
However, SMTP failed to validate instant message (IM) address like bob.archer#corpration.com which does exist.
Is there a method using java to validate instant message (IM) address.
Hope for your reply,
Thanks.
Apache commons provides a solid validator that can help you:
https://github.com/apache/commons-validator/blob/trunk/src/main/java/org/apache/commons/validator/EmailValidator.java
You can take a look the link above and use the methods of EmailValidator class.
Besides checking the email format (one or more alphanumeric and/or special characters followed by # with several more alphanumeric characters separated by at least one "."), the only way to validate anything is to actually attempt to use it, be that for email or for IM.
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);
}
In simple word, whenever I will send mail to anyone (within same mail server) it should appears some fake email address i.e. fakemail#gmail.com but, when they reply to this mail, it should come to my actual email address that is realname#gmail.com.
Note: My both email addresses will use same domain name, but only difference will be fake and real username for that email. I needed for the privacy issue. So, that nobody can reply to my mail directly until I send any mail to them.
Ask your mail server administrator to setup an email forwarding for you.
This is not a Java specific question. What you are asking for is called an
Email alias, and is a standard functionality on mail servers.
You can use a fake "from:" field and a valid "reply-to:" field, but the only people that will be fooled by this are people who would not know how to send spam anyway.
Even if you can (see below) mask the From field, you are giving away your email address in the Reply-To field, so you are still revealing your email address. (And if you weren't, it wouldn't be possible to reply.) So go with #Anony-Mousse and find a proper solution.
Now, please note that there are two "from" fields: one in the SMTP envelope and another in the message data.
It is quite uncommon for current mail servers to let you fake the SMTP from, though it may be possible if the server is using raw SMTP without authorization.
You are more likely to be allowed to send an email with a custom "From" in the message body (which is the one mail clients display, unless you look att all headers). However, the mail server may require that it matches the user you authenticated as.
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 to set the Return-Path to an email address other than Sender address using JavaMail?
The code below does what you want, and does it in the correct way. Reread what you yourself posted in the comment
From: RFC2821: 4.4 Trace Information
When the delivery SMTP server makes
the "final delivery" of a message, it
inserts a return-path line at the
beginning of the mail data. This use
of return-path is required; mail
systems MUST support it. The
return-path line preserves the
information in the
from the MAIL command. Here, final
delivery means the message has left
the SMTP environment. Normally, this
would mean it had been delivered to
the destination user or an associated
mail drop, but in some cases it may be
further processed and transmitted by
another mail system.
and a few lines later.
A message-originating SMTP system
SHOULD NOT send a message that already
contains a Return-path header.
If you carefully read this you will understand that only the final smtp-server/delivery agent is supposed to add the Return-Path header. It is not something you as client (trying to send a mail) should do. The final smtp-server will base the Return-Path header on the sender address of the envelope (SMTP MAIL FROM part).
So setting mail.smtp.from is the correct way to tell java that the envelope sender address should be different from the from part.
If you have troubles understanding what the different from's are just take a look at a telnet smtp-session. Where replyto#example.com should correspond to mail.smtp.from and from#example.com to m.addFrom(...);
telnet smtp.example.com 25
220 smtp.example.com ESMTP .....
helo computername
250 smtp.example.com Hello computername [123.123.123.123]
mail from:<replyto#example.com>
250 <replyto#example.com> is syntactically correct
rcpt to:<rcpt#foo.com>
250 <rcpt#foo.com> verified
data
354 Enter message, ending with "." on a line by itself
To: Joey <to#joey.com>
From: Joey <from#example.com>
Subject: Joey
Hey Joey!
.
250 OK id=....
Quit
props.put("mail.smtp.from", "replyto#example.com");
Session session = Session.getDefaultInstance(props, null);
MimeMessage m = new MimeMessage(session);
m.addFrom(InternetAddress.parse("from#example.com"));
I've experienced the same issue and found the only solution discussed putting property "mail.smtp.from" props.put("mail.smtp.from", "replyto#example.com");
Still this solution was not suitable for me because I'm sending lot's of e-mails from different users, so recreating session for each e-mail would be horrible for prodictivity.
So I found another solution after reading JavaMail sources:
1) Use SMTPMessage(extends MimeMessage) instead of MimeMessage.
2) Use setEnvelopeFrom(String) method.
3) Use SMTPTransport to send e-mail (I didn't try with others).
Here is a code example:
SMTPMessage message = new SMTPMessage(session);
message.setEnvelopeFrom("returnpath#hotmail.com");
...
transport.sendMessage(message, message.getAllRecipients());
I found that if the 'mail.protocol' property is set to something other than 'smtp' (like 'smtps'), then only the following would work:
props.put("mail.smtps.from", "replyto#example.com");
This allowed me to avoid using the SMTPMessage class as described in GiorgosDev's answer (classes in the 'com.sun' package aren't intended to be public API).