Java: Question regarding apache commons mail - java

I am trying to send an email using Apache commons Email API.
I installed hMailServer as my smtp server and created a domain test.com. I added an user, 'user1' .
I tried to send the mail using the below code
public static void sendSimpleMail() throws Exception {
Email email = new SimpleEmail();
email.setSmtpPort(25);
email.setDebug(false);
email.setHostName("localhost");
email.setFrom("user1#test.com");
email.setSubject("Hi");
email.setMsg("This is a test mail ... :-)");
email.addTo("abc#gmail.com");
email.setTLS(true);
email.send();
System.out.println("Mail sent!");
}
When my program runs, it prints , "Mail sent!". But it has been about 30 minutes, but I haven't got the mail in my inbox.
Is there something I am missing ? Is there delay due to network problems ?
Update:
I ran diagnostics and I got the below details.
I think the problem might be with outbound port.
Can anyone help me figure what is going wrong?

There could be a number of problems. Since you didn't get an exception in your Java code, most likely the E-mail has reached your hMailServer instance, but hasn't gotten past that. The documentation for hMailServer includes a number of troubleshooting suggestions.
My best guess would be that your ISP (or your local firewall) is blocking outbound port 25. Also, be sure to check your spam folder on gmail.

Well take a look at the error: "mail.hmailserver.com could not be resolved" (AKA not found). Are you sure that's the correct address? Are you sure there's a mail server there?
BTW unless you have some HOSTS file enteries, that second failed test with "test.com" will never work. Use a real mail server

Related

Getting Bounce email to another email address

I have setup mail server mail.example.com at smtp. Trying to send email with that, and the mail server send the mail correctly.
But if the email bounces, I want them to read at another email address, thats one gmail address. So for that i have tried doing this
SMTPMessage sMTPMessage = new SMTPMessage(mailSession);
sMTPMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
sMTPMessage.setFrom(new InternetAddress(from));
sMTPMessage.setSubject(subject);
sMTPMessage.setContent(body, contentType);
sMTPMessage.setEnvelopeFrom(bounceAddr);
SMTPTransport smtpTrans = new SMTPTransport(mailSession, new URLName("smtp", smtpServer, port, null, userid, password));
smtpTrans.connect();
smtpTrans.sendMessage(sMTPMessage, sMTPMessage.getAllRecipients());
System.out.println("transport "+smtpTrans.getReportSuccess());
smtpTrans.close();
But it didn't worked. and so I am not getting the bounce email information. Also I tried by setting mail.smtp.from property that also didn't worked.
Also I saw this
props.put("smtp.mail.from", bounceAddr);
But the from email address is not at the mail server, its on my gmail address. So is there any issue with from address domain ?
If you're using properties, make sure you don't have a typo in the property name, as you do above.
You can make sure that JavaMail is using the envelope From address that you specify by turning on JavaMail session debugging and looking for the MAIL command in the SMTP protocol in the debug output.
But the real problem is likely one of:
Not all mail servers honor this information, even though they
should.
One of the mail servers starting with your mail server and through to the destination mail server may not have forwarded this information as it forwarded your message, thus leaving the mail server that detects the problem without the information to return the error message to the proper place.
Your mail server may not be honoring this information because it's not an email address in the domain managed by your mail server.
Some of these problems may be fixable by checking the configuration and behavior of your mail server. But in the end you simply can't rely on always getting an error message back when you send to an invalid address.
You can find more information in the JavaMail FAQ.

JavaMail API connection to Mercury Mail server error

I am trying to connect to Mercury mail server with a java application and I am using JavaMail api. The connection is not successful and I’m getting this error in the log:
EHLO x.x.x.x
554 Invalid HELO format
Which means that it connects to the server but the helo format is not something that sever likes. I've tired to debug it and I got to this code in JavaMail “SMTPTransport” class which says:
serverOutput.write(cmdBytes);
serverOutput.write(CRLF);
serverOutput.flush();
and according to code:
private static final byte[] CRLF = { (byte)'\r', (byte)'\n' };
which seems consistent with RFC 821
I know that on windows \n has different meaning but I am not sure if this really is the root of problem? If it not then what can cause this?
I checked mail server with mail client and it works fine and I checked the code with James mails server and it also works fine!
JavaMail API version is: 1.4.5 (latest release)
Mercury/32 : 4.7
I did a Google search on 554 Invalid HELO format and got a ton of hits about your specific Mercury problem. It's a bug.
http://community.pmail.com/forums/thread/4136.aspx
http://www.mantisbt.org/bugs/view.php?id=9645
http://ellislab.com/codeigniter/forums/viewthread/153130/
etc...
You might need to check the logs on the server to figure out what it's really complaining about, but... One thing you can try is setting the mail.stmp.localhost property to the correct DNS host name for your machine. From the debug output it looks like your machine is unable to determine its own name and so is sending the IP address instead.
For me in a web project the PHPMailer() class was forcing Auth. Changing the Auth to false fixed it. Maybe check similar option for setting Auth to false in your language.
This is what I changed in my PHP code from true to false and adding sendmail_from in the php.ini file
$mail = new PHPMailer();
$mail->SMTPAuth = false;

Apache Commons Mail Erroring on Server Contact

I have a Java app that sends email via Apache Commons Email. The app works just find on my development environment, but when I deploy it to the server, I'm getting an error that Commons couldn't connect to the email server.
org.apache.commons.mail.EmailException: Sending the email to the following failed : 255.255.255.255 :587
...Error authenticating with server.
Just to be sure this wasn't my configuration, I've tried on 2 different email providers, 1) Our email company email provider 2) Gmail. Both work on my dev and both fail with the same message on the server.
I've tried several ports and IP combinations. I've turned SSL on and off. I've checked with our email provider. I'm left with a configuration of either the server or the network.
I work remotely so I'm not on the same network as our servers, but I the app works for me even if I am on VPN.
I've used telnet from within the server and I can connect to the email provider on the proper port.
Does anyone have anything else I can try?
Thanks.
Edit
The error I'm getting is an authentication error. Could there be a security setting to prevent the credentials from being passed?
Still sounds like a permission problem. Ports below 1024 are restricted. Start your app with sudo permission.
It appears I was missing 1 line of code.
Assume "email" is my mail object...
email.setSSL(true);
All is working now. I'm not sure why it wasn't working before.

550 Access denied - Invalid HELO name

I am using apace common mail API for sending html emails. following is my code.
public void sendHTMLMail(String to, String subject, String message , String from) throws EmailException
{
HtmlEmail email = new HtmlEmail();
email.setHostName(SMTP_HOST_NAME);
email.addTo(to);
email.setFrom(from, "just-flick");
email.setSubject(subject);
email.setSmtpPort(25);
email.setHtmlMsg(message);
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
But while running the program I am getting following error.
Exception in thread "main" org.apache.commons.mail.EmailException: Sending the e
mail to the following server failed : mail.just-flick.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
at org.apache.commons.mail.Email.send(Email.java:1267)
at bseller.mail.SendMail.sendHTMLMail(SendMail.java:105)
at bseller.mail.SendMail.main(SendMail.java:31)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 550 Access denied - Invali
d HELO name (See RFC2821 4.1.1.1)
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
... 3 more
Please help me to configure this problem.
Thanks
I've faced the same problem, and when i pass the name of client host everything became ok
i've add this line in my code:
props.put("mail.smtp.localhost", client or host name which connect to mail server);
Good Luck :)
This should be handled by the administrator of your mail server, not by you. Talk to whoever told you to connect to that mail server.
Maybe this will help:
email.getMailSession().getProperties().setProperty("mail.smtp.localhost", "www.example.com");
Of course replace www.example.com with the domain name of your host from where you send the mail.
However, as David Schwartz wrote, your mail configuration is not perfect either. Nowdays we do not submit mails to port 25. Port 587 is used for submission, which has more relaxed rules, although it may be necessary to authenticate yourself if your IP address is not white listed. Follow the link which was given by pst in his comment.
It is strange that JavaMail alone works, while Apache Commons Email does not, because I guess Commons Email also used JavaMail. This may indicate a bug somewhere but it would require further investigation.
One of your problem is that you do not know the exact HELO name you are sending. The following code may help to determine it, otherwise call your mail administrator (especially because he may advise you on port 587).
email.getMailSession().setDebug(true);
I was getting same error, it was fixed after enabling below properties:
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true

Sending email: failed to connect

I am using java to send mail.
I want to set the from mail id to xyz#chatmeter.com.
When i am using that for sending mail the following exception is generated..
Exception in thread "main" javax.mail.AuthenticationFailedException: failed to connect
at javax.mail.Service.connect(Service.java:322)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at sendmailtoclient.SendSMTP.sendMail(SendSMTP.java:125)
at sendmailtoclient.SendSMTP.main(SendSMTP.java:153)
I have used the correct password for the mail account.
although it I have used the xyz#gmail.com successfully.
If you have code please post to me.
Please help me.
Thanks in advance.
It looks like your credentials are wrong or not accepted by gmail.
You need SMTP server for sending mail. You can use one installed on your own machine
One free one is here.
Also check if you have added activation.jar
Mostly this error occurs from not having activation.jar and mail.jar on your classpath. Add them as a maven dependency or add in WEB-INF/lib and it should be fine.

Categories