GeeCON Prague 2014*
The moose likes Other JSE/JEE APIs and the fly likes Trying to send email with Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Trying to send email with Java" Watch "Trying to send email with Java" New topic
Author

Trying to send email with Java

Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Hello all,

I am trying to send an email using within the Java Application that have been working on as a learning exercise and for some reason it does not work. I am following a tutorial at tutorialspoint.com and seem to have everything working properly. I get no errors or exceptions and it even prints the set message successfully output that is near the bottom of the code snippet in the try block. The problem is that I never receive the email. I have tried three different email addresses that I have and haven't gotten an email at any of them. I have also checked the spam folder.

If it matters, I am using Mecury.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
So there's a mail server running on your local machine? That's what lines 9 and 15 imply. But since you want to send from a gmail.com account, you should use Gmail's mail server. The JavaMail section in the http://www.coderanch.com/how-to/java/JavaEnterpriseEditionFaq points to excellent introductory material from Oracle/Sun with many example codes.


Ping & DNS - my free Android networking tools app
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Ulf Dittmer wrote:So there's a mail server running on your local machine? That's what lines 9 and 15 imply. But since you want to send from a gmail.com account, you should use Gmail's mail server. The JavaMail section in the http://www.coderanch.com/how-to/java/JavaEnterpriseEditionFaq points to excellent introductory material from Oracle/Sun with many example codes.



Yes, I have Mercury running as part of the xxamp stack.

I don't necessarily want to send an email from a gmail account. I just left that because that is what was in the tutorial. I wanted be able to put whatever in the from. Something such as confirm@no-reply.com or however it is it is normally done.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
I see, I didn't know that Mercury is a mail server. In that case, it should write log files, no? That might be a good place to start. You could also turn on JavaMail debugging, which produces a lot of output about what's going on.

Your mail server will most likely not be seen as trustworthy if it is not set up properly for handling emails for a particular domain. That means emails it sends could be quietly discarded by other servers, certainly by the likes of Gmail, Yahoo and HotMail. That's what SPF is all about.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
OK. Thanks for the advice. I will take a look at your suggestions and post back what I figure out.
Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 323
    
    5

Raymond --

I tried your code with a LAN-attached SMTP server and it worked straight-away. If you can, run a network sniffer and see exactly what is going-on between the application and the email server. This is the dialogue that I captured:

220 mailserver.mcleodnet.home ESMTP Sendmail 8.13.8/8.13.8; Sat, 5 Apr 2014 09:29:12 -0700
EHLO fromage7.mcleodnet.home
250-mailserver.mcleodnet.home Hello fromage7.mcleodnet.home [192.168.100.125], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-AUTH LOGIN PLAIN
250-DELIVERBY
250 HELP
MAIL FROM:<webapp@gmail.com>
250 2.1.0 <webapp@gmail.com>... Sender ok
RCPT TO:<ron@mcleodnet.home>
250 2.1.5 <ron@mcleodnet.home>... Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
From: webapp@gmail.com
To: ron@mcleodnet.home
Message-ID: <685849732.0.1396715392632.JavaMail.ron@fromage7>
Subject: Transaction!
MIME-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 140404-0, 04/04/2014), Outbound message
X-Antivirus-Status: Clean
<h1>Transaction Confirmed</h1><br /><h3>Thank you for using our site!</h3><br /><p>Dear Ron,</p>
<p>Your transaction is confirmed.</p><br /><p>Thanks</p><br /><br /><br /><br /><br /><br /><p>Staff</p>
250 2.0.0 s35GTCxx007512 Message accepted for delivery
QUIT
221 2.0.0 mailserver.mcleodnet.home closing connection


Also, by default, JavaMail uses port 587 rather than port 25. Ensure that your mail server is configured to accept submissions on port 587, or change your app to use port 25.

Ron
Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 323
    
    5

Also, check to see if your email server is configured to required SMTP AUTH. Again, if you capture a network trace, this type of issue will be evident.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Ron McLeod wrote:Also, check to see if your email server is configured to required SMTP AUTH. Again, if you capture a network trace, this type of issue will be evident.




What network sniffer are you using? I tried looking at wireshark, there is is much going on, I don't even know what I am looking at. I tried to filter only smtp but when I did that and ran the app, I didn't see any traffic.

Below is the log from the mail server although I removed my address and added stars in place of it.


Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 323
    
    5

Your trace only has half the conversation - from the app to the server. It would be good to see the response from the server side as-well.

I used tcpdump and post-processed the pcap file with a home-build app. If you are running Linux or Mac you could also try using tshark or ngrep from the command line:
tshark -i lo -s 8888 -nn port 587 or port 25
ngrep -d lo -s 8888 -W byline -q -P \* port 587 or port 25



Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Ron McLeod wrote:Your trace only has half the conversation - from the app to the server. It would be good to see the response from the server side as-well.

I used tcpdump and post-processed the pcap file with a home-build app. If you are running Linux or Mac you could also try using tshark or ngrep from the command line:
tshark -i lo -s 8888 -nn port 587 or port 25
ngrep -d lo -s 8888 -W byline -q -P \* port 587 or port 25





I am not running Linux or Mac and looking through tcpdump, which looks like it's for Linux?

I did find WinDump and tried running it, but still didn't see any traffic as I ran the app. Should I be filtering for something or running certain commands? Admittedly, I am not very familiar with using the command line.

I'm not sure if this would matter, but when working through a similar program using PHP, I had no problem receiving the emails although I did simply use the mail function rather than going through a local mail server like I am attempting to do now.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Raymond Gillespie wrote:I don't necessarily want to send an email from a gmail account.


Then your from-address should be in the domain which your e-mail server belongs to. You haven't mentioned anything about your e-mail server beyond the fact that it exists. But you can't just use any server to send e-mails from any e-mail address, you know. Otherwise, for example, if I knew your e-mail address then I could set up a mail server and start sending e-mails which appeared to be from you. Obviously that's a bad thing, so e-mail servers nowadays tend to check that the server matches the from-address.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Another thing to note is that java mail messages often get flagged as spam. So if you send it to a gmail account you may never see it, if you send it to you home account check the spam folder.

Joe


It's not what your program can do, it's what your users do with the program.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Paul Clapham wrote:
Raymond Gillespie wrote:I don't necessarily want to send an email from a gmail account.


Then your from-address should be in the domain which your e-mail server belongs to. You haven't mentioned anything about your e-mail server beyond the fact that it exists. But you can't just use any server to send e-mails from any e-mail address, you know. Otherwise, for example, if I knew your e-mail address then I could set up a mail server and start sending e-mails which appeared to be from you. Obviously that's a bad thing, so e-mail servers nowadays tend to check that the server matches the from-address.


So is it possible to set up an e-mail server on my local machine and be able to send emails? If so, what would the email address even be?
Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 323
    
    5

Do you want to use Google as your mail transfer agent? It would only take a few additional lines to get that working.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Yeah that would work for me.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
I have been looking around trying to figure out how to configure the Mercury mail server. I figured I could just set it up through a domain I use for testing and such but the problem I continue to run into is this step:

Go to "Configuration" -> "MercuryC SMTP Client"

This is a problem because my installation of Mercury does not have a MercuryC SMTP Client and all the other ones I have does not have the configuration of C.

Anyone konw anything about this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Are you following this checklist? http://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows

It looks to me like your Mercury SMTP server is designed to delegate to a real SMTP server, at least that's what #24 says. So just configure it to delegate to whatever server you have available. The example in the checklist shows how to use GMail, if you don't have another alternative available.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Paul Clapham wrote:Are you following this checklist? http://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows

It looks to me like your Mercury SMTP server is designed to delegate to a real SMTP server, at least that's what #24 says. So just configure it to delegate to whatever server you have available. The example in the checklist shows how to use GMail, if you don't have another alternative available.


That's exactly what I was looking at. The problem begins at step 22 because I do not have a MercuryC SMTP or anything that resembles the steps configuration that MercuryC has.

"22. Now we have the important one "MercuryC SMTP Client"

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well then you're going to have to download it or install it or something. I'm competent to discuss sending e-mail from Java but we're a long way from that now, I have no idea how to configure your e-mail server. So you're on your own -- personally at this point I would go to the other plan of connecting directly to Gmail.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
Paul Clapham wrote:Well then you're going to have to download it or install it or something. I'm competent to discuss sending e-mail from Java but we're a long way from that now, I have no idea how to configure your e-mail server. So you're on your own -- personally at this point I would go to the other plan of connecting directly to Gmail.


How do I connect directly to Gmail?

While I want to figure this server out, I would like to know how to do that also. That would as least confirm that my code is working and it would be something nice to know anyway.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Raymond Gillespie wrote:How do I connect directly to Gmail?


That's one of the questions in the JavaMail FAQ.
Raymond Gillespie
Ranch Hand

Joined: Oct 08, 2012
Posts: 123
I got this working using gmail. While not ideal, it certainly works.

Now to figure out how to use this mail server properly.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So far, so good. What you've done so far is useful because now you know you have working code. If you tried to write code and run it through an untested server at the same time, and it didn't work, you wouldn't know whether it was the untested code or the untested server.
 
GeeCON Prague 2014
 
subject: Trying to send email with Java