Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSP电子邮件发送错误_Java_Jsp_Email_Smtp_Jakarta Mail - Fatal编程技术网

Java JSP电子邮件发送错误

Java JSP电子邮件发送错误,java,jsp,email,smtp,jakarta-mail,Java,Jsp,Email,Smtp,Jakarta Mail,我正在创建一个web应用程序来向某个收件人发送电子邮件。当我运行这段代码时,我会遇到这样的异常 org.apache.jasper.jaspereException:在第27行处理JSP页面/two.JSP时发生异常 24: props.put("mail.smtp.auth", "true"); 25: props.put("mail.smtp.port", "465"); 26: 27: Session mailSession = Session.

我正在创建一个web应用程序来向某个收件人发送电子邮件。当我运行这段代码时,我会遇到这样的异常

org.apache.jasper.jaspereException:在第27行处理JSP页面/two.JSP时发生异常

24:        props.put("mail.smtp.auth", "true");
25:        props.put("mail.smtp.port", "465");
26: 
27:        Session mailSession = Session.getDefaultInstance(props,
28:                new javax.mail.Authenticator() {
29:                      protected PasswordAuthentication  
30:                         getPasswordAuthentication() {
我该怎么办?这是我完整的jsp代码

            <html>
            <body>

            <%@ page import="java.util.Properties" %>               
            <%@ page import="javax.mail.Message" %>
            <%@ page import="javax.mail.MessagingException" %>
            <%@ page import="javax.mail.PasswordAuthentication" %>
            <%@ page import="javax.mail.Session" %>
            <%@ page import="javax.mail.Transport" %>
            <%@ page import="javax.mail.internet.InternetAddress" %>
            <%@ page import="javax.mail.internet.*" %>


            <%
            Properties props = new Properties();
                    props.put("mail.smtp.host", "smtp.gmail.com");
                    props.put("mail.smtp.socketFactory.port", "465");
                    props.put("mail.smtp.socketFactory.class",
                            "javax.net.ssl.SSLSocketFactory");
                    props.put("mail.smtp.auth", "true");
                    props.put("mail.smtp.port", "465");

                    Session mailSession = Session.getDefaultInstance(props,
                            new javax.mail.Authenticator() {
                                protected PasswordAuthentication 
                                      getPasswordAuthentication() {
                                    return new PasswordAuthentication
                                      ("senderUsername","senderPassword");
                                }
                            });

                    try {

                        Message message = new MimeMessage(mailSession );
                        message.setFrom(new InternetAddress("senderemail@gmail.com"));
                        message.setRecipients(Message.RecipientType.TO,
                                InternetAddress.parse("recipient@gmail.com"));
                        message.setSubject("hi");
                        message.setText("text contrnt" +
                                "\n\n Test email");

                        Transport.send(message);

                        System.out.println("Done");

                    } catch (MessagingException e) {
                        throw new RuntimeException(e);
                    }
            %>
            </body>
            </html>
有什么帮助吗? 谢谢。

Session mailSession=Session.getInstanceprops, 新的javax.mail.Authenticator{ 保护密码身份验证 getPasswordAuthentication{
返回新密码验证 ,; } };

使用Session.getInstance而不是Session.getDefaultInstance。

转到并禁用两步验证,然后再试一次。

首先,修复这些问题

接下来,如果您仍然无法理解,请使用这些命令并发布调试输出

您可能希望用try/catch来包围代码,以确保不会丢失正在抛出的异常


您不会说您正在使用哪个应用服务器,但如果它不是Java EE应用服务器,请确保JavaMail jar文件位于服务器的lib目录或web应用程序的lib目录中。

我已经尝试过了。没有错误。代码正在编译和运行。但电子邮件没有发送。错误是什么?我没有收到上述代码的任何错误,我正在接收电子邮件。当我运行此代码页时,将在浏览器中打开并加载它,而不发送电子邮件。那么该怎么办呢?您能看到控制台中打印的Done字符串吗?返回新密码身份验证myUsername,myPassword;message.setFromnewInternetAddressmyEmailAddress@gmail.com; message.setRecipientsMessage.RecipientType.TO,InternetAddress。parserecipient@gmail.com; 我设置了这个地址。我只举了一个例子。在我的程序中,我使用了我的原始地址。我尝试了这种方式。同样的事情也在发生。我不能断定是什么错误。我试过很多教程。但同样的事情发生了。如何解决这个问题。这是我申请时遇到的令人厌烦的问题