Send/Receive Email from within Web Application? - java

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.

Related

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

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.

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.

sending mail in testNG without server in java

please how to test sending email without real server in java knowing that email is send in a project A and I want to test sending in project B
Email cannot be "sent" without an email server.
Email is modeled after a physical mail system, with a few changes. The "sending" is the equivlent to the dropping the letter off at a post office. It is not possible to do point-to-point email.
Email clients then open their "PO Box" or "Mailbox" on the server, and perhaps (it is optional) then downloads the mail into the local machine.
In fact, the "sending" of an email is a number of back-and-forth communication with the email server using SMTP. Basically sending an email consists of telling the server hello, then asking about it's capabilities, and eventually asking it to validate addresses and accept a body (and optionally attachments).
So, if you really need to send a mail to a product in project B, and you cannot rely on a standalone server, you need to create a server in project B.
I think that you should consider mocking the mail sending. Depending on the frameworks you use, you should be able to find a good way to do it. Here is a blog illustrating a possible mechanism: http://blog.nutpan.com/2012/03/mock-testing-for-java-mail.html?m=1
You can also consider using a mocking framework like easymock or mockito to test it.

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.

What is needed to add emailing capabilities to web application?

I have java web application to which I'd like to add emailing capabilities, however, I'm unsure what is needed to accomplish this. Specifically I want my app to be able to:
Send emails confirming sign-up
Allow users to send emails to one another, using my app's domain i.e. dan#my-app.com
From my research it seems I'll need a mail transfer agent (MTA) like Postfix and possibly a IMAP server like Courier; but I don't understand the need for the IMAP server.
Thanks.
You need code inside your web app to create and dispatch the email into the SMTP-world. Usually JavaMail is used for this, and you can either enclose it in your web application or (preferred) have the web container provide a correctly configured instance through JNDI. This is vendor specific.
If you do not have a SMTP-server for JavaMail to connect to (frequently this is Exchange for Windows shops), you can either get one running (ask your IT administrator) or use Google Mail or Hotmail or others if it is ok for your web application to send mail through them. It is a bit tricky to use GMail as a SMTP-server, but when set up correctly works very well.
You will need the SMTP-server, as it handles all the boring details regarding MX records and resending if the SMTP-server does graylisting, etc. etc.
Oh, and IMAP is for getting delivered mail, not sending mail. You don't need it.
If it's a Java web app, then the server part is a servlet. Given an email message sent from a client form, your server needs to send that text off as an email.
There's code in the Java EE stack to do this, or you can specifically download JavaMail. This will allow your programs to act as mail clients.
Your MTA receives messages from your servlet and sends them to the users. So far so good.
But you also need a postbox, i.e. the equivalent of a mail in-box for your users. Postfix, QMail and others offer a basic "flat" mailbox model, where mail is simply stored until the client picks it up, and then (usually) deleted. Access is via POP3. IMAP offers a lot more organizational capability, i.e. the ability to specify hierarchies of nested mailboxes, to transfer mails between them and so on. You probably won't want to create a GUI front end to all that complexity, so I'd guess you don't really need an IMAP server. You do, however, want a relatively simple POP3 server to allow your servlet to access the mailboxes via TCP/IP. This is usually part of the "standard" email server packages.
To have your own domain known to the world, you need access to the MX records of your DNS service, i.e. you have to set up one or two of your hosts, on an Internet-facing address, to be your post office.
Finally, if you want to save yourself a lot of trouble, be very careful in configuring your MTA (SMTP server) such that there is no chance for it being used as an open relay. i.e. it should not be possible for your users to send mail to the outside world in general (or hackers will find a way to abuse your Web interface to do this), and mail from the Internet should not reach your users. Most importantly, there should be no way for mail from the Internet to be forwarded to someplace else in the Internet. Find an open relay testing service (they're free) on the 'net and get one to run a test on your configuration once you think you're done.
EDIT:
Looking at Thorbjorn's answer, I realized you probably don't want your users receiving their mail through your app; they probably already have email providers and accounts of their own. In that case, you don't need to worry about inbox capability or a POP3 server. You could consider offering full email services at your domain but that's a very thankless job and if you have any choice, leave that dirty work to GMail, Yahoo, Hotmail and their ilk. Whatever service you provide will never please your customers enough, and you'll be fighting spam and other crime every day.
For starters your server has to have mailing abilities. In linux land sendmail is usually what this will be.
Additionally, check out javaMail.
http://www.oracle.com/technetwork/java/index-jsp-139225.html

Categories

Resources