Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
为什么发件人在JSP中使用javaMail API时不显示电子邮件?_Java_Jsp_Email_Smtp_Jakarta Mail - Fatal编程技术网

为什么发件人在JSP中使用javaMail API时不显示电子邮件?

为什么发件人在JSP中使用javaMail API时不显示电子邮件?,java,jsp,email,smtp,jakarta-mail,Java,Jsp,Email,Smtp,Jakarta Mail,我已经编写了一个示例JSP代码,用于使用JavaMail API向另一个用户发送电子邮件。该表单包含发件人电子邮件框、主题框和评论部分。所有这三个html字段都将使用jsp文件中的request.getParameter进行检索。电子邮件是从主机smtp.gmail.com发送的,但是当我登录gmail时,我看到它是以我自己的身份发送的,换句话说,就是为自己发送电子邮件。下面是我一直在研究的示例代码: '<%@ page language="java" contentType="text/

我已经编写了一个示例JSP代码,用于使用JavaMail API向另一个用户发送电子邮件。该表单包含发件人电子邮件框、主题框和评论部分。所有这三个html字段都将使用jsp文件中的request.getParameter进行检索。电子邮件是从主机smtp.gmail.com发送的,但是当我登录gmail时,我看到它是以我自己的身份发送的,换句话说,就是为自己发送电子邮件。下面是我一直在研究的示例代码:

'<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<%@ page isErrorPage="true"  %>

<%
   String result;
   // Recipient's email ID needs to be mentioned.
   String to = "toABC@gmail.com";
   final String username = "toABC@gmail.com";
   final String password = "somePassword";
   // Sender's email ID needs to be mentioned
   String from = request.getParameter("senderEmail");



   // Get system properties object
   Properties properties = new Properties();  

   // Setup mail server

    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");


   Session mailSession = Session.getInstance(properties,
              new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication()         {
                return new PasswordAuthentication(username, password);
                }
              });

    try{
       // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);
       // Set From: header field of the header.
       message.setFrom(new InternetAddress(from));
       // Set To: header field of the header.
       message.addRecipient(Message.RecipientType.TO,
                                new InternetAddress(to));
       // Set Subject: header field
       message.setSubject(request.getParameter("subject"));
       // Now set the actual message
       message.setText(request.getParameter("content"));
       // Send message
       Transport.send(message);
       result = "Sent message successfully....";
     }catch (MessagingException ex) {
       ex.printStackTrace();
       result = "Error: unable to send message....";
       out.println("<br>");
       out.println("<p>" + ex.getMessage() +" </p>");

     }
  %>


   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" http://www.w3.org/TR/html4/loose.dtd">
 <html>
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
  </head>


 <body>
   <center>
      <h1>Send Email using JSP</h1>
   </center>
   <p align="center">
 <% 
     out.println("Result: " + result + "\n");
  %>
 </p>
 </body>
 </html>`
下面是我的HTML页面:

    <html>
<body>
 <form action="htmlEmail.jsp" method="post">
        <table border="0" width="35%" align="center">
           <caption><h2>Send New E-mail</h2></caption>
            <tr>
                <td width="50%">Sender email address </td>
                 <td><input type="text" name="senderEmail" size="50"/></td>
           </tr>
           <tr>
                <td>Subject </td>
                <td><input type="text" name="subject" size="50"/></td>
          </tr>
          <tr>
                <td>Content </td>
                <td><textarea rows="10" cols="39" name="content"></textarea>    </td>
           </tr>
           <tr>
                <td colspan="2" align="center"><input type="submit" value="Send"/></td>
           </tr>
          </table>

    </form>




</body>
 </html>

在HTML页面中,我输入了发件人的电子邮件地址,当我在服务器上运行上述代码后打开Gmail帐户时,我认为该地址将显示为发件人电子邮件。相反,我收到了来自toABC@gmail.com. 我的问题是,为什么我没有收到通过电子邮件发送的电子邮件fromXYZ@gmail.com在html表单中输入的?。正如您在代码中看到的,我使用message.setfrom作为发件人。然后,我会在电子邮件中将其显示为发件人。感谢您的帮助。

一般来说,邮件服务器不允许您发送来自任意地址的邮件。如果您想从其他特定地址发送邮件,请参阅。

是否确定从中找到了正确的值?请尝试将其发送到内容中,以确保它没有将from设置为null并默认为电子邮件。@stvcisco…我已将from字符串concat为message.setTextrequest.getParametercontent++from。我可以看到fromXYZ@gmail.com在我收到的电子邮件内容中。记住,它是内容,而不是发送者。如上所述,发送者仍然是我自己。@Bill…我或多或少地使用下面的链接来理解Javamail api和jsp。下面链接中的代码表示,用户可以从电子邮件地址获得这两个地址,但我不确定发布代码的人是否能够接收发件人的电子邮件。我已经通过了你的链接,我会尝试一下,但在此之前,我会在我的代码做更多的调整,然后将再次张贴在这里。