【问题标题】:Cannot send an email via jsp in gmail无法通过 gmail 中的 jsp 发送电子邮件
【发布时间】:2013-09-03 01:14:03
【问题描述】:

我在一个名为 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>
[....]

代码基于此处显示的第二个TLS issue when sending to gmail through JavaMail,但尽管据说可以工作,但我无法让它工作。我还测试了输入 root 作为用户和密码并传递用户和密码,如图所示,但它不起作用。

但是好像没有发什么,可能是收件箱收不到邮件但我怀疑。

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

当然,页面的编译很顺利。

感谢您的帮助。

【问题讨论】:

  • 你好@user2638180你怎么能在客户端javascript函数中使用服务器端jsp代码,也许你应该从基础开始

标签: java jsp


【解决方案1】:

好吧,我们从哪里开始?

首先,您必须了解桌面应用程序之间的区别,桌面应用程序通过点击、双击、按键等事件触发操作。网络应用程序需要表单、请求、响应,如果您使用 ajax,嗯,事件 + ajax(这会引导您了解请求、响应和表单)。

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

希望对你有帮助!

问候, 埃弗拉

【讨论】:

    【解决方案2】:

    尝试使用下面给出的代码发送电子邮件,但将此代码用于 scriptlet 不在 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); }
    

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

    【讨论】:

    • 我认为这不是一个好习惯,我的意思是,将服务器端代码与演示代码混合在一起。我知道你可以设置一些字段或类似的东西,但是像电子邮件发送这样特定的东西,在脚本中使用它只是一个坏主意,特别是如果你的应用程序比预期的要大。
    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 2019-02-04
    • 2013-02-03
    • 2012-01-07
    • 2023-03-19
    相关资源
    最近更新 更多