无法向SMTP主机发送电子邮件

时间:2014-05-14 08:40:03

标签: java email jms

我正在尝试发送没有SSL / TLS的电子邮件,但收到以下异常:

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)

发送电子邮件的代码: 我已经按照其他帖子的建议放了System.setProperty(" java.net.preferIPv4Stack"," true"),我使用的是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();
    }

1 个答案:

答案 0 :(得分:-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>

对于Servlet代码,请访问This链接。

对于上述代码,只需接受来自用户的收件人和发件人电子邮件ID,密码,主题和正文部分。