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

时间:2011-03-17 08:10:27

标签: html jsp

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>

2 个答案:

答案 0 :(得分:2)

在您的代码中尝试以下内容

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});

答案 1 :(得分:1)

此代码工作

String host = "smtp.gmail.com";
user="youEmail@gmail.com";
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();