发送使用JSP电子邮件发送使用JSP电子邮件(Sending email using JSP)

2019-05-12 11:48发布

这个问题我发疯。 我有以下代码:

<html>
<body>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
String host = "exchsrv2";
String to = "alan@domain.com";
String from = "apeince@domain.com";
String subject = "test";
String messageText = "body test";

Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");
Session mailSession = Session.getDefaultInstance(props, null);

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
%>
</body>
</html>

好了,问题是,我得到以下错误:

javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: exchsrv2, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.alan_jsp._jspService(alan_jsp.java:113)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我知道Exchange服务器是存在的。 我可以telnet到它就好了。 我的Exchange服务器安装程序不要求身份验证。 我有一个程序,工作在C#/。NET的罚款,并能正常工作,所以我知道这个问题是不是在Exchange服务器。 我在做什么错在这里?

Answer 1:

那么随便,我会说有尝试连接时的身份验证问题。 您没有提供任何用户名或密码,除非您的Exchange服务器不需要用户名和密码。


更新:如果使用JDK 7请参见下面的文章,它解决了这个问题:

缺陷-使用VPN时JDK7权限与插座被拒绝

“更多的周围挖,似乎VPN客户端有IPv6的禁用,这是造成与JDK7的问题。如果我用下面的标志-Djava.net.preferIPv4Stack =真我再也看不到的错误。这是预期的解决方法还是这一个问题?”


public class MailTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws MessagingException {
        String host = "smtp.gmail.com";
        String to = "myEmail@gmail.com";
        String from = "myEmail@gmail.com";
        String subject = "test";
        String messageText = "body test";

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", "25");

        // If using authentication, otherwise comment out
        props.put("mail.smtp.auth", "true");

        // Gmail requires TLS, your server may not
        props.put("mail.smtp.starttls.enable", "true");

        Session mailSession = Session.getDefaultInstance(props, null);

        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(messageText);

        Transport transport = mailSession.getTransport("smtp");

        //connect with authentication
        //transport.connect(host,"myUsername" , "myPassword");

        //connect without authentication
        transport.connect();
        transport.sendMessage(msg, address);

        transport.close();

        System.out.println("Mail was sent to " + to);
        System.out.println(" from " + from);
        System.out.println(" using host " + host + ".");

    }
}


Answer 2:

如果你不想更改首选IP堆栈IPv4的替代解决方案可能是使用的IPv6地址的DHCP名称,例如代替主机:

String host = "fe80::b84d:1ed2:5329:de87%10";

错误的原因的确是IPv6的是拖欠的Windows从JDK 7开始当你试图连接到一个IPv4地址,然后在它之下将使用IPv4映射的IPv6地址的盖(请参阅从Oracle网站这个博客 )。 但通常这应该是没有问题的,如果您的Exchange服务器支持IPv6。 然而,我在一个类似的问题,切换到JDK 7奇怪的后通过MS Exchange 2010服务器发送Java代码中的邮件时是根据运行在MS TechNet此链接它支持IPv6。 因为我不想通过设置标志-Djava.net.preferIPv4Stack =真正启动应用程序时,是不是一种选择,默认我们的完整应用的IPv4,接受的解决方案的IP堆栈。 从而改变了主机名到一个有效的IPv6在我的情况下更好的解决方案。 这不是100%我清楚为什么这个工作,而不是DHCP名称。



Answer 3:

      <%
      String host = "smtp.gmail.com";
      String from="send-from@gmail.com";//Your E-mail-Id
      String pass="xxxxxx";      //your e-mail account password

String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = "send-to@gmail.com"; //recipient E-mail-Id
String from = "send-from@gmail.com"; // Your E-mail Id
String subject ="test mail";
String messageText = "hello abc";
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");//port number 465 for Secure (SSL) and we can also            use port no 587 for Secure (TLS)
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)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html");


Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);


 try {
      transport.sendMessage(msg, msg.getAllRecipients());
      out.println("Send Success");
      boolean WasEmailSent = true; // assume it was sent
     }

catch (Exception err) {
                       boolean WasEmailSent = false; 
                      }
                  transport.close();
      %>


文章来源: Sending email using JSP