Can't set the email sender in JavaMail - Spring [duplicate] - java

I want to send an email from A to B, with HEADER and CONTENT through gmail.
How to do that by PHP?
I've specified the FROM (from#example.com), but when I receive the email, it's still from my gmail account (abc#gmail.com).
$mail->From = "from#example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("abc12#163.example", "Josh Adams");// name is optional
$mail->AddReplyTo("abc12#qq.example", "Information");
How do I change the FROM part?

The short answer - you can't.
Google rewrites the From and Reply-To headers in messages you send via it's SMTP service to values which relate to your gmail account.
The SMTP feature of gmail isn't intended to be an open or relay service. If it allowed any values for the From header, it would significantly dilute Google's standing with spam services, as there would be no way to verify the credentials of the sender.
You need to consider alternatives. How are you planning to host your script/application/website when it's finished: virtually every hosting solutions (shared/vps/dedicated server) will come pre-configured with an email transfer solution: be it sendmail or postfix on *nix, or IIS on Windows.
If you are intent on using gmail then you could:
Setup a dedicated myapp#gmail.com account
If you own the domain you are supposedly sending from, use the free gmail for domains, and setup a myapp#mydomain.example account.
====
Edit June 2015
It was suggested that GMail does allow sending via different addresses. As far as I can tell, this is for sending via the GMail wep app, and utilises your existing external SMTP server, which is not relevant to the original question.
====
Edit Nov 2013
Seeing as this is still getting a trickle of votes. A quick update.
Google have withdrawn their free GMail for domains. There are plenty of other free services around. One of note is Mandrill - a one-to-one email service intended for transactional emails (e.g. ecommerce orders etc.). It's ran by MailChimp, who pretty much know all there is to know about sending email at volume. They also give you 12k/month free, which is rather nice.

This question and correct answer may be relevant:
When using Gmail for SMTP, can you set a different "from" address?
Gmail requires you to validate From addresses before sending mail as that email address. So you need to add a new sender in your personal gmail account and validate it.
Doing so will allow you to authenticate with youremail#gmail.com and send email from from#example.com

Unlike everyone else, I'll take the plunge and make the assumption that by letters you mean emails...
But I'm not sure what you are getting at when you mention that it should include "Headers and Content". Do you want to forward emails? Do you want the emails from A to appear as though they came from B's gmail account in the headers? Are you building some sort of gmail client?
The easiest way to send an email with PHP is with the mail function. This example comes straight from their documentation:
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
If you want the headers to appear from A's gmail and not to simply change the from/reply to part, you'd have to use gmail as the SMTP server. I don't know if you can set that at the script level.

The answer above are not quite correct.
You are definitely able to specify any senders as long as you own the other email address.
As the help page explains:
On your computer, open Gmail.
In the top right, click Settings.
Click the Accounts and import or Accounts tab.
In the "Send mail as" section, click Add another email address.
Enter your name and the address you want to send from.
Click Next Step and then Send verification.
For school or work accounts, enter the SMTP server (for example, smtp.gmail.com or smtp.yourschool.edu) and the username and password on that account.
Click Add Account.
Once that email is added successfully,
you can send email on the behalf of the new email address in gmail.
Google will not rewrite your from email in this way while you're sending email via Google SMTP.

You need to go to GMAIL settings and add new alias.
You will be asked SMTP information, which is basically useless, since you are using SMTP to send email, BUT the catch is that if your alias is on Google Suite domain it will be added just with simple email confirmation!
Once you have the alias there, you can change "From" header in your SMTP email.
NOTE: You cannot change the "From" address to whatever#dude.example, that's just how Gmail works and is the reason it's trusted.

If the reason you want to use gmail is because you don't want to set up an MTA (the reason you stated in a comment to this answer), you have 2 options:
If the web server is at your
home/work place; use your ISP's
smtp-server
If the web server is at a dedicated
hosting center, ask them what
smtp-server to use.

Related

How to send "no-reply" email to users?

I have a spring MVC application where I want to send a no-reply email after each user registration.
To send a no-reply email, what all configuration needs to do in service providers like Gmail or anything, we need to write extra code in java.
or is there any 3rd party tool available which handles this?
There is no magic for this. A "no reply" email is one which has a Reply-to: address that goes to a mailbox that will be ignored or discarded. Usually you use a mailbox name that indicates to the user that their reply will be ignored.
For example:
Reply-to: no-replies#example.com # assuming 'example.com' is your domain.
Typically you don't want replies to the "no reply" email to bounce. But that is an option too.
To send a no-reply email , what all configuration need to do in service provider like gmail or anything we need to write extra code in java.
No configuration is needed. You just code the mail sender (e.g. your Java application) to include a "Reply-to:" header.
However, you will need to set up the mail server that hosts the "no reply" mailbox so the the emails are discarded.
Note: it would be a bad idea to use a 3rd-parties email domain (real or fake) for your "no reply" email address. Why? Because some people reply to the "no reply" address anyway. If the domain is owned by someone else, there is nothing to stop them from setting up a mail server to capture the emails ... and harvest confidential information.

How to configure an app on Google App Engine to Receive Email on any email address?

As we know if we want to enable our app on App Engine to receive emails, we need to first configure an email address of the format string#appid.appspotmail.com in web.xml file. However, I wanted to know if it is possible to use an email address of the form string#domain.com which can be used to receive emails for the application. Like you can map you appid#appspot.com to your own domain, and it works fine but wanted to know if it is possible do it for emails also somehow.
Just direct your mail server for domain.com to forward string#domain.com to string#your-appid.appspotmail.com or your-appid#appspot.com. The inbound email processor doesn't care what the original "to" address is -- it will process it regardless. You have access to the headers programmatically, so you can decide to treat it differently depending on what email it came in through, if you want.

Send/Receive Email from within Web Application?

I have an Order Management (Web) Application (in Java/Java EE).
The application users want to Send Receive email communication to Customers who placed the Order, from within the Web-Application. The Email Trail must be associated with the Order.
The use-case is:
User opens the Order Detail Page. Order info. is displayed.
On that page - Clicks on Email Icon, and it will show up all Email Communication (sent/received) for that Order till date.
For that Order - You can see new incoming mails, reply to mails or send out new mails to the Customer - all related to that Order.
Questions:
Sending out emails is easy, but how to receive emails within the app?
What is the email account here - A common Email Account called orders#myorg.com (and based on subject line/some-header emails are filtered etc.) or is it orderid#myorg.com (in which case new email acct creation is reqd per order) or ?
We already have Microsoft Exchange Server via which company email travels. Could we leverage that in someway or do we need to setup a new Mail Server?
Any ideas are welcome.
If you're willing to take on the dependency, Spring Integration can comfortably read email from a designated server on a polling (POP3, IMAP) or event-driven basis (IMAP-IDLE). [1 & 3]
[2] You can use a dedicate mail account and filter the mail sent to downstream channels based on the subject (or other field) of the incoming mail. The following snippet from the Spring site illustrates this:
<int-mail:imap-idle-channel-adapter id="customAdapter"
store-uri="imaps://some_google_address:${password}#imap.gmail.com/INBOX"
channel="receiveChannel"
should-mark-messages-as-read="true"
java-mail-properties="javaMailProperties"
mail-filter-expression="subject matches '(?i).*Spring Integration.*'"/>
Where mail-filter-expression filters the email that will be flushed down receiveChannel. For all interested parties (channels), you'll have one <int-mail:imap-idle-channel-adapter/> listening to your Exchange server.
While it's not cumbersome to use, I'd recommend you look at a short overview of EAI according to spring and of EAI in general
You need the following.
Java Mail API to programmatically send emails through some exchange server.
I would suggest to use a dedicated email server to receive application specific emails. You can do this by using James. You can send and receive emails through this software.
If you use your existing Microsoft Exchange Server, you can always use redirect to your application specific email server.
Receive emails
Here is example code to read an email Are there any good short code examples that simply read a new gmail message?
Email account here
For each user create an email account and have user's web app credentials used as credentials for email as well.
Use hashed order id as part of subject to relate each email chain to specific order.
Microsoft Exchange Server
Use either exJello is a JavaMail provider (http://www.exjello.org/)
OR Use JavaMail API with DavMail Gateway (http://davmail.sourceforge.net/)
From a user point of view I think keeping a specific subject is more difficult. I would suggest a sub domain like myapp.myorg.com or a new domain like myapp.com
Either ways have a catch all so that all mails go to a specific email like orders#myapp.myorg.com
Then your script can check the real TO. This might be more natural and 'cool' -> each order has its own mail id! On top of that use James or other mail software to get delivery to your code.

How to make reply of an email becomes a HttpRequestServlet to a web application?

I got a requirement which I don't know how to get started.
Requirement:
The application will send emails to clients (using org.springframework.mail.javamail.JavaMailSender) with same "from email address" as support#mydomain.com. Also the email content will have some hidden information such as client_id. If the client reply back to that email, I need to get that as an HttpServletRequest to a Servlet so that I can process it.
EDIT:
This web application does not have google app engine setup, but my requirement is matching similar to this link.
When someone replies to the mail you sent him that mail will go to the POP3 server configured for your domain. From this SMTP server you can read this mail using the Java Mail API. You can have a Spring timer task to keep polling this mailbox after every few seconds.
I would look at the below resources to see how to read mail from the POP3 server -
Using JavaMail API -
http://metoojava.wordpress.com/2010/03/21/java-code-to-receive-mail-using-javamailapi
Using Spring -
http://blog.solidcraft.eu/2011/04/read-emails-from-imap-with-spring.html
Better and popular solution is to have a link in your email and asking user to click it and asking specifically not to reply to this email (from address can be noreply#domain.com)
For getting a servlet call in an email reply, you need to use
http://javamail.kenai.com/nonav/javadocs/com/sun/mail/pop3/package-summary.html
Keep polling for new emails in your reply address(for eg support#domain.com) using POP, and when new email comes trigger a httprequest(HttpClient? ).
On a related note you can add user_id and similar stuff using a + in reply email address
For eg support#domain.com and support+uid_21_catid_32#domain.com will deliver to same address. Everything after + is ignored. But you will get this info in reply email event as to address and decide on who is the user.This could be a security concern for sure if not properly implemented.

Send email with custom "from" address

In my open source and free online application, the tool allows people to send emails to their audience. For this purpose, I use java mail API and Gmail SMTP. However, when sending, the "from" address always is my account that I use to authenticate into Gmail and not the user's email address. This leads to a lot of confusion and problems.
I understand Gmail's philosophy to not allow this (to prevent abuse), but it is a serious limitation from my app's perspective.
Are there any free/ cheap online email services other than Gmail that allow programmatic interfacing and allow programs to send emails with a specified "from" address?
The hosting service for your application should provide you with a SMTP server that you should use to send emails. But as mentioned, forging From is the most common sign of spam and most likely such emails will be filtered out by spam filters or SMTP servers on the route.
Why don't you set the reply-to address for the email so that the users still reply to the correct email address?
Therefore you keep the from address as your gmail address but set the reply-to address to be the users address.
Something like:
msg.setReplyTo(new InternetAddress("someone#yahoo.com"));

Categories

Resources