Reputation: 2697
What is wrong with this code? I'm trying to send a email on my localhost with hMailServer, but it is not working. While this code is working for the Gmail SMTP server.. I might think the error is in my hMailServer, but I can't find it..
try{
String host = "127.0.0.1";
String from = "[email protected]";
String pass = "1q2w#E$R";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
String[] to = {"[email protected]"}; // added this line
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception e) {
e.printStackTrace();
}
This is the error I'm getting:
javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at nl.company.cms.login.emailservice.TestMail.main(TestMail.java:71)
I'm using hMailServer..
Upvotes: 2
Views: 8967
Reputation: 3
I can confirm that only adding the VM argument for Tomcat :
-Djava.net.preferIPv4Stack=true
can send a fake mail with "Fake SMTP Server" on my localhost from my webapp through JavaMail API
Upvotes: 0
Reputation: 547
I can confirm that by only implementing the suggestion provided by @Quantas of adding parameter "-Djava.net.preferIPv4Stack=true" in the "run configurations" in ECLIPSE, my problem of getting exception mail.Messaging Exception has been resolved now.
From last 2 days i was getting following exception while using "Fake SMTP mail server" to trap the mails sending out from my application running in eclipse.
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
Now the problem have been completely resolved by just putting this little piece of code into "VM ARGUMENTS" in "run configurations" of ECLIPSE LUNA
-Djava.net.preferIPv4Stack=true
As shown in snapshot below
This suggests the java to prefer using IPV4 stack instead of IPV6 (which it prefers previously)
Upvotes: 0
Reputation: 51
I confirm that only this parameter "-Djava.net.preferIPv4Stack=true", written in the catalina.bat of Tomcat, helped me to send an email from my Spring web app into my local hMailServer instance. Enviroment: Windows 8, Java 7u60.
Upvotes: 1
Reputation: 11
If you are using Xampp make sure Mercury is running on your machine on your machine because port 25 won't work with javaMail if mercury is not started.
Upvotes: 1
Reputation: 673
If you are using Java 7 then I would try adding this as a JVM parameter when you start your application:
-Djava.net.preferIPv4Stack=true
This may be necessary because Java is now trying to use IPv6 so we need to tell it to prefer the IPv4 stack instead.
Upvotes: 3