Sending email using JSP

本小妞迷上赌 提交于 2019-11-27 09:47:52

Well offhand I would say there is an authentication issue when trying to connect. You are not providing any username or password, unless your exchange server doesn't require username and password.


UPDATE: If using JDK 7 see the following post, it resolved this issue:

Defect - JDK7 Permission Denied with Sockets when using VPN

"More digging around and it seems the VPN client has IPv6 disabled which is causing issues with JDK7. If I use the following flag -Djava.net.preferIPv4Stack=true I no longer see the errors. Is this workaround expected or is this an issue?"


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 + ".");

    }
}

If you do not want to change the preferred IP stack to IPv4, an alternative solution might be to use the IPv6 address for the host in stead of the DHCP name, e.g.:

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

The reason of the error is indeed that IPv6 is defaulted on Windows starting from JDK 7. When you attempt to connect to an IPv4 address then under the covers it will use an IPv4-mapped IPv6 address (see this blog from the Oracle website). But normally this should be no problem, if your exchange server supports IPv6. However, I ran in a similar issue when sending a mail from Java code through an MS Exchange 2010 Server after switching to JDK 7. The strange thing is that according to this link on MS Technet it supports IPv6. Since I did not want to default the IP stack of our complete application to IPv4, the accepted solution by setting the flag -Djava.net.preferIPv4Stack=true when starting the application was not an option. Thus changing the host name to a valid IPv6 was a better solution in my case. It's not 100% clear to me why this works and not the DHCP name.

      <%
      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();
      %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!