Community Discussions and Support
SMTP Mercury JavaMail API connection error

JavaMail is a Java API used to send and receive email via SMTP, POP3 and IMAP. JavaMail is built into the Java EE platform, but also provides an optional package for use in Java SE. You may refer to the code below.

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

// Send a simple, single part, text/plain e-mail

public class TestEmail {

public static void main(String[] args) {

// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!

String to = "sendToMailAddress";

String from = "sendFromMailAddress";

// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!

String host = "smtp.yourisp.invalid";

// Create properties, get Session

Properties props = new Properties();

// If using static Transport.send(),

// need to specify which host to send it to

props.put("mail.smtp.host", host);

// To see what is going on behind the scene

props.put("mail.debug", "true");

Session session = Session.getInstance(props);

try {

// Instantiate a message

Message msg = new MimeMessage(session);

//Set message attributes

msg.setFrom(new InternetAddress(from));

InternetAddress[] address = {new InternetAddress(to)};

msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject("Test E-Mail through Java");

msg.setSentDate(new Date());

// Set message content

msg.setText("This is a test of sending a " +

"plain text e-mail through Java.\n" +

"Here is line 2.");

//Send the message

Transport.send(msg);

}

catch (MessagingException mex) {

// Prints all nested (chained) exceptions as well

mex.printStackTrace();

}

}

}//End of class

<p>JavaMail<b> </b>is a <a href="http://www.keepautomation.com/products/java_barcode/" mce_href="http://www.keepautomation.com/products/java_barcode/">Java <span class="mw-redirect">API</span></a> used to send and receive email via <span class="mw-redirect">SMTP</span>, <span class="mw-redirect">POP3</span> and <span class="mw-redirect">IMAP</span>. JavaMail is built into the <span class="mw-redirect">Java EE</span> platform, but also provides an optional package for use in <span class="mw-redirect">Java SE</span>. You may refer to the code below.</p><pre class="de1"><span class="kw2">import</span> <span class="co2">java.util.*</span><span class="sy0">;</span> <span class="kw2">import</span> <span class="co2">javax.mail.*</span><span class="sy0">;</span> <span class="kw2">import</span> <span class="co2">javax.mail.internet.*</span><span class="sy0">;</span> <span class="kw2">import</span> <span class="co2">javax.activation.*</span><span class="sy0">;</span> <span class="co1">// Send a simple, single part, text/plain e-mail</span> <span class="kw2">public</span> <span class="kw2">class</span> TestEmail <span class="br0">{</span> <span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">void</span> main<span class="br0">(</span><span class="kw21">String</span><span class="br0">[</span><span class="br0">]</span> args<span class="br0">)</span> <span class="br0">{</span> <span class="co1">// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!</span> <span class="kw21">String</span> to = <span class="st0">"sendToMailAddress"</span><span class="sy0">;</span> <span class="kw21">String</span> from = <span class="st0">"sendFromMailAddress"</span><span class="sy0">;</span> <span class="co1">// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!</span> <span class="kw21">String</span> host = <span class="st0">"smtp.yourisp.invalid"</span><span class="sy0">;</span> <span class="co1">// Create properties, get Session</span> <span class="kw46">Properties</span> props = <span class="kw2">new</span> <span class="kw46">Properties</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> <span class="co1">// If using static Transport.send(),</span> <span class="co1">// need to specify which host to send it to</span> props.<span class="me1">put</span><span class="br0">(</span><span class="st0">"mail.smtp.host"</span>, host<span class="br0">)</span><span class="sy0">;</span> <span class="co1">// To see what is going on behind the scene</span> props.<span class="me1">put</span><span class="br0">(</span><span class="st0">"mail.debug"</span>, <span class="st0">"true"</span><span class="br0">)</span><span class="sy0">;</span> Session session = Session.<span class="me1">getInstance</span><span class="br0">(</span>props<span class="br0">)</span><span class="sy0">;</span> <span class="kw2">try</span> <span class="br0">{</span> <span class="co1">// Instantiate a message</span> Message msg = <span class="kw2">new</span> MimeMessage<span class="br0">(</span>session<span class="br0">)</span><span class="sy0">;</span> <span class="co1">//Set message attributes</span> msg.<span class="me1">setFrom</span><span class="br0">(</span><span class="kw2">new</span> InternetAddress<span class="br0">(</span>from<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span> InternetAddress<span class="br0">[</span><span class="br0">]</span> address = <span class="br0">{</span><span class="kw2">new</span> InternetAddress<span class="br0">(</span>to<span class="br0">)</span><span class="br0">}</span><span class="sy0">;</span> msg.<span class="me1">setRecipients</span><span class="br0">(</span>Message.<span class="me1">RecipientType</span>.<span class="me1">TO</span>, address<span class="br0">)</span><span class="sy0">;</span> msg.<span class="me1">setSubject</span><span class="br0">(</span><span class="st0">"Test E-Mail through Java"</span><span class="br0">)</span><span class="sy0">;</span> msg.<span class="me1">setSentDate</span><span class="br0">(</span><span class="kw2">new</span> <span class="kw166">Date</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span> <span class="co1">// Set message content</span> msg.<span class="me1">setText</span><span class="br0">(</span><span class="st0">"This is a test of sending a "</span> + <span class="st0">"plain text e-mail through Java.<span class="es0">\n</span>"</span> + <span class="st0">"Here is line 2."</span><span class="br0">)</span><span class="sy0">;</span> <span class="co1">//Send the message</span> Transport.<span class="me1">send</span><span class="br0">(</span>msg<span class="br0">)</span><span class="sy0">;</span> <span class="br0">}</span> <span class="kw2">catch</span> <span class="br0">(</span>MessagingException mex<span class="br0">)</span> <span class="br0">{</span> <span class="co1">// Prints all nested (chained) exceptions as well</span> mex.<span class="me1">printStackTrace</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> <span class="br0">}</span> <span class="br0">}</span> <span class="br0">}</span><span class="co1">//End of class</span></pre>

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 127.0.0.1

 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 

<p class="MsoNormal">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:</p> <p class="MsoNormal">EHLO 127.0.0.1</p> <p class="MsoNormal"> 554 Invalid HELO format</p> <p class="MsoNormal">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 “<span style="font-size: 10pt; line-height: 115%; font-family: Consolas; background-color: silver; background-position: initial initial; background-repeat: initial initial; ">SMTPTransport</span><span style="font-size: 10pt; line-height: 115%; font-family: Consolas; ">”</span> class which says:</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; "><span style="font-size: 10.0pt;font-family:Consolas;color:#0000C0;background:silver;mso-highlight:silver">serverOutput</span><span style="font-size: 10pt; font-family: Consolas; ">.write(cmdBytes);</span><span style="font-size:10.0pt;font-family:Consolas"><o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; "><span style="font-size: 10.0pt;font-family:Consolas;color:#0000C0;background:silver;mso-highlight:silver">serverOutput</span><span style="font-size: 10pt; font-family: Consolas; ">.write(</span><i><span style="font-size:10.0pt;font-family:Consolas;color:#0000C0">CRLF</span></i><span style="font-size: 10pt; font-family: Consolas; ">);<o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; "><span style="font-size: 10.0pt;font-family:Consolas;color:#0000C0;background:silver;mso-highlight:silver">serverOutput</span><span style="font-size: 10pt; font-family: Consolas; ">.flush();<o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; "><span style="font-family: Consolas; font-size: 10pt; ">and according to code</span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; ">private static final byte[] CRLF = { (byte)'\r', (byte)'\n' };</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; ">which seems consistent with RFC 821 </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; "><o:p> 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?</o:p></p> <p class="MsoNormal">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!</p> <p class="MsoNormal">JavaMail API version is: 1.4.5 (latest release) </p> <p class="MsoNormal">Mercury/32:  4.7 </p>

[quote user="Fred.jand"]

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 127.0.0.1

 554 Invalid HELO format

[/quote]

Probably a compliance filter from MercuryS.  To find out exactly what is wrong, turn on session logging in Configuration / MercuryS, General tab, and see the actual HELO sent.

(Don't forget to turn the logging off afterwards.)

[quote user="Fred.jand"] <P class=MsoNormal>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:</P> <P class=MsoNormal>EHLO 127.0.0.1</P> <P class=MsoNormal> 554 Invalid HELO format</P> <P>[/quote]</P> <P>Probably a compliance filter from MercuryS.  To find out exactly what is wrong, turn on session logging in Configuration / MercuryS, General tab, and see the actual HELO sent.</P> <P>(Don't forget to turn the logging off afterwards.)</P>

Try EHLO [127.0.0.1] (brackets are required).

/Rolf 

 

 

<p>Try EHLO [127.0.0.1] (brackets are required).</p><p>/Rolf </p><p> </p><p> </p>
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
With selected deselect posts show selected posts
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft