使用Java Mail发送邮件

时间:2016-01-20 01:11:28

标签: java jsp smtp

地狱伙计们! 我想用jee发送电子邮件。这是我使用的代码,但它不起作用。我使用的代码如下。

的index.html          `                              Java Mail                                

                <thead><tr> <td colspan="3" align="center">
                <b> Send Mail </b> </td> </tr> </thead>
                <tr> <td> To </td> <td> : </td>
                    <td> <input type="text" name="to" value="" /> </td>
                </tr>
                <tr>
                    <td> Subject </td> <td> : </td>
                    <td> <input type="text" name="subject" value="" /> </td>
                </tr>
                <tr>
                    <td> Message </td> <td> : </td>
                    <td> <textarea name="message" rows="8" cols="30">
                    </textarea></td>
                </tr>
                <tr>
                    <td colspan="3" align="center">
                    <input type="submit" value="Send Mail" />

                    <input type="reset" value="Reset" />
                    <td>
                </tr>

        </table>
    </form>
</body>

`

我创建了一个包含发件人的电子邮件ID和密码以及接收者的电子邮件ID的bean Mail.java
     `包jMail;     import javax.mail。;     import javax.mail.internet。;     import java.util。*;

public class Mail {

      private String to;
      private String from;
      private String message;
      private String subject;
      private String smtpServ;
      public int sendMail(){
    try
    {
        Properties props = System.getProperties();


         props.put("mail.transport.protocol", "smtp" );
          props.put("mail.smtp.starttls.enable","true" );
          props.put("mail.smtp.host",smtpServ);
          props.put("mail.smtp.auth", "true" );
          Authenticator auth = new SMTPAuthenticator();
          Session session = Session.getInstance(props, auth);
          // -- Create a new message --
          Message msg = new MimeMessage(session);
          // -- Set the FROM and TO fields --
          msg.setFrom(new InternetAddress(from));
          msg.setRecipients(Message.RecipientType.TO,    InternetAddress.parse(to, false));
          msg.setSubject(subject);
          msg.setText(message);
          // -- Set some other header information --
          msg.setHeader("MyMail", "Mr. XYZ" );
          msg.setSentDate(new Date());
          // -- Send the message --
          Transport.send(msg);
          System.out.println("Message sent to"+to+" OK." );
          return 0;
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
      System.out.println("Exception "+ex);
      return -1;
      }
     }

     private class SMTPAuthenticator extends javax.mail.Authenticator {
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        String username =  "monemail@gmail.com";           // specify your email id here (sender's email id)
        String password = "password";                                      // specify your password here
        return new PasswordAuthentication(username, password);
    }
    }

public String getTo() {
    return to;
}

public void setTo(String to) {
    this.to = to;
}

public String getFrom() {
    return from;
}

public void setFrom(String from) {
    this.from = from;
}


public String getMessage() {
    return message;
}


public void setMessage(String message) {
    this.message = message;
}


public String getSubject() {
    return subject;
}


public void setSubject(String subject) {
    this.subject = subject;
}


public String getSmtpServ() {
    return smtpServ;
}


public void setSmtpServ(String smtpServ) {
    this.smtpServ = smtpServ;
}

 }`

sendMail.jsp          `                    

      </head>
      <body>
      <jsp:useBean id="mail" scope="session" class="jMail.Mail" />
      <jsp:setProperty name="mail" property="to" param="to" />
       <jsp:setProperty name="mail" property="from"    value="monemail@gmail.com" />
        <jsp:setProperty name="mail" property="smtpServ" value="monemail@gmail.com" />
        <jsp:setProperty name="mail" property="subject" param="subject" />
         <jsp:setProperty name="mail" property="message" param="message" />

     <%
           String to = mail.getTo();
            int result;
             result = mail.sendMail();
             if(result == 0){
             out.println(" Mail Successfully Sent to "+to);
              }
          else{
          out.println(" Mail NOT Sent to "+to);
                }  
              %>
           </body>
            </html>`

0 个答案:

没有答案