我们如何在jsp中发送电子邮件

时间:2017-05-21 08:43:36

标签: java jsp email

这是我的代码,但它不能正常工作,请帮助我。请提供任何相关的代码或guid。谢谢

    <div style="margin-left:600px; width:550px;background-color:lightgreen">

         <form class="form-group" style=" margin-right:200px; padding: 30px 0px 0 25px;  background-color:white">


             <p style="margin: -30px 0 10px  70px; font-size: 20px">Sending Email </p>

                <input type="email" class="form-control" placeholder="To" required name="to" style="width:300px" /><br />

                <input type="email" class="form-control" placeholder="From" required name="from" style="width:300px" /><br />
                <input type="text" class="form-control" placeholder="subject:" required name="subject" style="width:300px" /><br />
                <textarea placeholder="body" required name="body" cols="32" rows="7" style="border:1px gray solid;border-radius:5px;"></textarea><br />

                <input type="submit" value="send" name="btn"   style="width: 300px; height: 35px; border: 1px solid gainsboro; border-radius: 4px; background-color:limegreen; color: white"/><br /><br />
    <%
         String from=request.getParameter("from");
          String to=request.getParameter("to");
           String bod=request.getParameter("body");
        String subject=request.getParameter("subject");
try{
  Session mailSession = Session.getInstance(System.getProperties());
  Transport transport = new SMTPTransport(mailSession,new URLName("smtp.gmail.com"));
  transport.connect("smtp.gmail.com",465,null,null);
  MimeMessage m = new MimeMessage(mailSession);
  m.setFrom(new InternetAddress(from));
  Address[] toAddr = new InternetAddress[] {
  new InternetAddress(to)
  };
  m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
  m.setSubject(subject);
  m.setSentDate(new java.util.Date());
  m.setContent(bod, "text/plain");
  transport.sendMessage(m,m.getAllRecipients());
  transport.close();
  out.println("Thanks for sending mail!");
}
catch(Exception e){
  out.println(e.getMessage());
  e.printStackTrace();
}
%>
</div>
        </div>

显示错误是:无法连接到SMTP主机:smtp.gmail.com,端口:465,响应:-1

0 个答案:

没有答案