通过java邮件通过jsp发送消息的问题

发布于 2024-10-24 02:38:29 字数 2538 浏览 2 评论 0原文

enter code here

这是 MailForm.html

<body>
  <form action="sendMail.jsp" method="post">
    <table cellspacing="2" cellpadding="2" border="1">
      <tr>
        <td>To:</td>
        <td>
          <input type="text" name="to" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td>From:</td>
        <td>
          <input type="text" name="from" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td>Subject</td>
        <td>
          <input type="text" name="subject" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <textarea cols="40" rows="10" name="body"></textarea>
        </td>
      </tr>
      <tr>
        <td>
          <input type="submit" name="submit" value="Submit">
          <input type="Reset">
        </td>
      </tr>
    </table>
  </form>
</body>
</html>


----------------------------------

    enter code here

这是SendMail.jsp JSP JavaMail 示例

<body>

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

<%
    String host = "smtp.gmail.com";
    String to = request.getParameter("to");
    String from = request.getParameter("from");
    String subject = request.getParameter("subject");
    String messageText = request.getParameter("body");
    boolean sessionDebug = false;

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

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

%>
    </table>
  </body>
</html>
enter code here

It's MailForm.html

<body>
  <form action="sendMail.jsp" method="post">
    <table cellspacing="2" cellpadding="2" border="1">
      <tr>
        <td>To:</td>
        <td>
          <input type="text" name="to" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td>From:</td>
        <td>
          <input type="text" name="from" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td>Subject</td>
        <td>
          <input type="text" name="subject" size="30" maxlength="30">
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <textarea cols="40" rows="10" name="body"></textarea>
        </td>
      </tr>
      <tr>
        <td>
          <input type="submit" name="submit" value="Submit">
          <input type="Reset">
        </td>
      </tr>
    </table>
  </form>
</body>
</html>


----------------------------------

    enter code here

It's SendMail.jsp

JSP JavaMail Example

<body>

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

<%
    String host = "smtp.gmail.com";
    String to = request.getParameter("to");
    String from = request.getParameter("from");
    String subject = request.getParameter("subject");
    String messageText = request.getParameter("body");
    boolean sessionDebug = false;

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

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

%>
    </table>
  </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-10-31 02:38:29

在您的代码中尝试

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

以下内容,您还需要将身份验证器传递给 Session.getDefaultInstance

,例如

Session mailSession = Session.getDefaultInstance(props, {pass authenticator here});

try the folowing in your code

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

together with these you also need to pass the authenticator to the Session.getDefaultInstance

like

Session mailSession = Session.getDefaultInstance(props, {pass authenticator here});
自由如风 2024-10-31 02:38:29

这段代码有效

String host = "smtp.gmail.com";
user="[email protected]";
pass="yourPassword";

 String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = email_to_address; // out going email id
String from = email_from_address; //Email id of the recipient
String subject = email_subject;
String messageText = email_body;
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)};
 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");
transport.connect(host, user, pass);


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

catch (Exception err) {
WasEmailSent = false; // assume it's a fail
}
transport.close();

this code work's

String host = "smtp.gmail.com";
user="[email protected]";
pass="yourPassword";

 String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = email_to_address; // out going email id
String from = email_from_address; //Email id of the recipient
String subject = email_subject;
String messageText = email_body;
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)};
 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");
transport.connect(host, user, pass);


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

catch (Exception err) {
WasEmailSent = false; // assume it's a fail
}
transport.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文