-1

我在一个名为 clave.jsp 的文件中有以下代码:

@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.detelexia.bbdd.Datos" %>
<%@ page import="com.detelexia.web.Utils" %>
<%@ page import = "java.text.*" %>

<%@ page import="java.io.*,java.util.*,javax.mail.*, javax.mail.Service"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
 [....]
<div><a href="javascript:void();" onclick="email(); return false;" class="button button-alt">ENVIAR</a></div>
<script>
function email()  
{

Properties props = new Properties();
    props.put("mail.smtp.host",  "smtp.gmail.com");  
        props.put("mail.from","existingdirection@gmail.com");
        props.put("mail.smtp.port", "587");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");

    Session session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("existingdirection@gmail.com", "correctpassword");
        }
    });
    try {
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom();
        msg.setRecipients(Message.RecipientType.TO,
                          "existingdirection@gmail.com");
        msg.setSubject("JavaMail hello world example");
        msg.setSentDate(new Date());
        msg.setText("Hello, world!\n");
        Transport.send(msg);
     } catch (MessagingException mex) {

     }

}
</script>
[....]

该代码基于此处显示的第二个通过 JavaMail 发送到 gmail 时出现的 TLS 问题,但尽管据说可以工作,但我无法让它工作。我还测试了输入 root 作为用户和密码并传递用户和密码,如图所示,但它不起作用。

但它似乎没有发送任何东西,也许是接收收件箱无法收到消息但我怀疑它。

知道我做错了什么吗?我已经尝试检查该代码以及更多代码,但它们似乎都不起作用,我不知道为什么,看起来一切正常。

当然,页面的编译非常顺利。

谢谢你的帮助。

4

2 回答 2

0

好吧,我们从哪里开始?

首先,您必须了解桌面应用程序之间的区别,桌面应用程序是由单击、双击、按键等事件触发的操作。而 Web 应用程序则需要表单、请求、响应(如果您使用的话)在 ajax 上,嗯,事件 + ajax(它引导你了解请求、响应和表单)。

我认为您需要检查 Web 应用程序开发的基础知识。就像表示层的请求/响应生命周期、表单、javascript、css 和 html。然后您需要检查 Java 的基础知识(JSP、servlet、过滤器、类、对象)。根据您想做的事情(MVC、HMVC、DAO、DI/IoC、Observable Observer),了解设计模式或至少如何/何时使用/实现它们对您有好处。

希望能帮助到你!

问候, 埃弗拉

于 2013-09-02T17:14:03.830 回答
-1

尝试下面给出的代码发送电子邮件, 但将此代码用于 scriptlet <% code goes here %> 而不是 JavaScript 或任何其他客户端脚本函数

我希望它会奏效。

String host="smtp.gmail.com";
final String user="it1006813052@gmail.com";//change accordingly
final String password="wooofqrzcjkjdmlaezr";//change accordingly

String to="it1006813052@gmail.com";//change accordingly

 //Get the session object

try { 
  Properties props = new Properties();
  props.put("mail.smtp.user",user); 
  props.put("mail.smtp.password", password);
  props.put("mail.smtp.host", host); 
  props.put("mail.smtp.port", "25"); 
  props.put("mail.debug", "true"); 
  props.put("mail.smtp.auth", "true"); 
  props.put("mail.smtp.starttls.enable","true"); 

  props.put("mail.smtp.EnableSSL.enable","true");

  props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
   props.setProperty("mail.smtp.socketFactory.fallback", "false");   
  props.setProperty("mail.smtp.port", "465");   
  props.setProperty("mail.smtp.socketFactory.port", "465"); 

  Session session = Session.getDefaultInstance(props,
  new javax.mail.Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(user,password);
     }
   });

 //Compose the message
 try {
  MimeMessage message = new MimeMessage(session);
  message.setFrom(new InternetAddress("rohan@bhediya.com"));
  message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
 message.setSubject("whass up?");
 message.setText("This is simple program of sending email using JavaMail API");
 // add attatchment

 MimeBodyPart messageBodyPart = new MimeBodyPart();

    Multipart multipart = new MimeMultipart();

    messageBodyPart = new MimeBodyPart();
    String file = "F:/emb3.png";
    String fileName = "emb3";
    DataSource source = new FileDataSource(file);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(fileName);
    multipart.addBodyPart(messageBodyPart);

    message.setContent(multipart);     


//send the message
 Transport.send(message);

   out.println("message sent successfully...");

     } catch (MessagingException e) {e.printStackTrace();}
  }
  catch(Exception ex) { System.out.println(ex); }

如果您不想附加任何东西,您可以删除附件部分

于 2013-09-02T15:40:58.910 回答