1

I am trying to send an email without SSL/TLS but receiving following exception:

javax.mail.MessagingException: Could not connect to SMTP host: mymail.test.com, port: 25;
 nested exception is:java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)

My code that send email : I have put System.setProperty("java.net.preferIPv4Stack" , "true") also as suggested in other posts and I m using java-6.

            String smtpHostServer = "mymail.test.com";
    String emailID = "user@test.com";
    System.setProperty("java.net.preferIPv4Stack" , "true");
    Properties props = System.getProperties();
    props.put("mail.smtp.host", smtpHostServer);
    props.put("mail.smtp.auth", "false");
    props.put("mail.smtp.starttls.enable", "false");

    Session session = Session.getDefaultInstance(props);
try {
        MimeMessage msg = new MimeMessage(session);
        // set message headers
        msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
        msg.addHeader("format", "flowed");
        msg.addHeader("Content-Transfer-Encoding", "8bit");

        msg.setFrom(new InternetAddress("noreply@test.com",
                "NoReply-JD"));

        msg.setReplyTo(InternetAddress.parse("noreply@test.com",
                false));

        msg.setSubject(subject, "UTF-8");

        msg.setText(body, "UTF-8");

        msg.setSentDate(new Date());

        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(toEmail, false));
        System.out.println("Message is ready");
        Transport.send(msg);

        System.out.println("EMail Sent Successfully!!");
    } catch (Exception e) {
        e.printStackTrace();
    }
user3082228
  • 51
  • 1
  • 8
  • Umm, you are not the owner of test.com right :p? I guess there is no SMTP running there? I tried to telnet the host on 25 port and I was unable to do so.. – Serhiy May 14 '14 at 08:41
  • well if you want code in jsp, I can provide you. just tell me do you have account on test.com? or you just wrote it for example. – Aditya Ekbote May 14 '14 at 08:53
  • @Serhiy I have renamed mail host name as I can not put it on net, but I have tried nslookup on it and it works. – user3082228 May 14 '14 at 08:55
  • @AdityaEkbote I have renamed mail host name, but yes I do have account on it. Account is account so it's password is not with me. – user3082228 May 14 '14 at 08:57
  • if you want to send email through gmail or rediffmail you can use their smtp servers at port 465 which is universally open for all. Should i post code in servlet or jsp? – Aditya Ekbote May 14 '14 at 08:58
  • @AdityaEkbote it is email through my client's server SMTP server. we do not have any proper account on it but they have provided us one userid only. – user3082228 May 14 '14 at 09:02
  • And you sure that their mail server allows unsecure protocols? – Serhiy May 14 '14 at 09:04
  • I agree with Serhiy. If you are sending email by web application, it is must for you to provide certain level of security. Be sure of security concerns. Below I am posting code in JSP. – Aditya Ekbote May 14 '14 at 09:09
  • possible duplicate of [JavaMail API to iMail -- java.net.SocketException: Permission denied: connect](http://stackoverflow.com/questions/12901475/javamail-api-to-imail-java-net-socketexception-permission-denied-connect) – Volker Stolz May 14 '14 at 09:19
  • Is it helpful to you? Try putting it in your application. :-) – Aditya Ekbote May 14 '14 at 09:22
  • @ShiDoiSi I have clearly mentioned that I have applied that fix in JavaMail API to iMail -- java.net.SocketException: Permission denied: connect. – user3082228 May 14 '14 at 10:00
  • @user3082228 Did you also try setting the VM-switch? Maybe it makes a difference. – Volker Stolz May 14 '14 at 10:32
  • possible duplicate of [JavaMail: How to solve SocketException?](http://stackoverflow.com/questions/8671319/javamail-how-to-solve-socketexception) – Raedwald May 14 '14 at 19:04
  • @ShiDoiSi yes I have tried telnet and VM-switch also – user3082228 May 15 '14 at 11:03

1 Answers1

-1
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
  <head>
  <title>Simple Mail Program</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
 <body>
<%@page import="java.sql.*"%>
<%@page import="javax.mail.*"%>
 <%@page import="javax.mail.internet.*"%>
  <%@ page import="java.io.*"%>
  <%@ page import="java.sql.*"%>
  <%@page import="java.util.*"%>
  <%@ page import="java.math.BigInteger"%>



 <%
String host = "smtp.tctinfotech.com";
//host = smtp_server; //"smtp.gmail.com"; user = jsp_email; //"YourEmailId@gmail.com" // email id to send the emails
//pass = jsp_email_pw; //Your gmail password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to_add = request.getParameter("receiver");
String subject =request.getParameter("subject"); 
String messageText =request.getParameter("body"); 
String password = request.getParameter("pwd");
String from =request.getParameter("email_id");
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to_add) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html"); // use setText if you want to send text
Transport transport = mailSession.getTransport("smtp");
System.setProperty("javax.net.ssl.trustStore", "conf/jssecacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "admin");
transport.connect(host, from, password);
try 
{
    transport.sendMessage(msg, msg.getAllRecipients());
    out.println("sent");
    //WasEmailSent = true; // assume it was sent
}
catch (Exception err) 
{
    //WasEmailSent = false; // assume it's a fail
    out.println("Error" + err.getMessage());
}
transport.close();
%>
 </body>
 </html>

For Servlet Code visit This Link.

For above code just accept receiver and sender email id, password, subject and body part from user.

Aditya Ekbote
  • 1,493
  • 3
  • 15
  • 29