user1914116
user1914116

Reputation: 11

Send Email from one address server to another server using Java Mail API

I have an online form that allows users to email a complaint to the company. To test it I have used gmail smtp as my host. I have no problem receiving the message to the designated email account when the sender is also a gmail but I want the "From" to not be limited to just gmail accounts. It appears that smtp is only good for sending emails from the same server?

Example: My form works great if the from is [email protected] and the company email is [email protected].

However if [email protected] is entered for the sender, the receiver [email protected] never gets it.

Any help would be greatly appreciated. I can provide my code as well if that is needed.

Upvotes: 1

Views: 3835

Answers (3)

mikeslattery
mikeslattery

Reputation: 4119

Your problem is a common security restriction when using SMTP. Outgoing SMTP email can only contain a "mail from" address belonging to the sender. If you break this rule, your email may be considered SPAM.

The following will allow your recipient to reply to an alternate address.

Properties properties = new Properties();
props.put("mail.smtp.from", "[email protected]");
Session session = Session.getDefaultInstance(props, null);
MimeMessage m = new MimeMessage(session);
m.addFrom(InternetAddress.parse("[email protected]"));
m.setReplyTo(InternetAddress.parse("[email protected]")); 

See also

Upvotes: 1

Bill Shannon
Bill Shannon

Reputation: 29971

It's probably better to send the message to your company's mail server using the identity of the user who owns the application on the server, and include the information that the customer provides in the online form as data in the message you send. The message won't look like it came from the customer, but then it really didn't come from the customer since it wasn't sent using the customer's mail server.

Upvotes: 0

ppsreejith
ppsreejith

Reputation: 3438

Well you will have to own the other email as well as set it to work with gmail, Check here for more details.

Upvotes: 0

Related Questions