Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaMail API时未收到任何响应_Java_Email_Jakarta Ee_Jakarta Mail - Fatal编程技术网

使用JavaMail API时未收到任何响应

使用JavaMail API时未收到任何响应,java,email,jakarta-ee,jakarta-mail,Java,Email,Jakarta Ee,Jakarta Mail,我正在尝试使用Gmail smtp服务器从网页向我的电子邮件id发送电子邮件。我在stackoverflow上尝试了各种答案。不幸的是,它们都没有起作用 `public void sendMail(String msg){ Properties props= System.getProperties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.auth", "true

我正在尝试使用Gmail smtp服务器从网页向我的电子邮件id发送电子邮件。我在stackoverflow上尝试了各种答案。不幸的是,它们都没有起作用

   `public void sendMail(String msg){

    Properties props= System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtps.debug", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");
    //props.put("mail.smtp.ssl.enable", "true");
    //props.put("mail.transport.protocol", "smtp");

    Authenticator authenticate= new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(username, password);
            }
            };
    @SuppressWarnings("deprecation")
    Session mailSession= Session.getInstance(props, authenticate);
    mailSession.setDebug(true);

    Message mailMessage= new MimeMessage(mailSession);

    try {
        mailMessage.setFrom(new InternetAddress(mailFrom));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        mailMessage.setSubject(subject);
        mailMessage.setText(msg);

        Transport trans= mailSession.getTransport("smtp");
        trans.connect("smtp.gmail.com", username, password);
        trans.sendMessage(mailMessage, mailMessage.getAllRecipients());
        trans.close();

    } catch (Exception ex) {
        Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
    }

}` 
我正在尝试上面写的代码

调试:setDebug:JavaMail版本1.5.4 信息:调试:getProvider正在返回 javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,sun Microsystems,Inc] 信息:调试SMTP:useEhlo true,useAuth true 信息:调试SMTP:尝试连接到主机SMTP.gmail.com,端口465,isSSL false

而且,这是我得到的输出,没有例外,没有消息,甚至没有电子邮件到我的电子邮件地址。 参考在线教程,我使用JavaMail API 1.5.4 Java 8和GlassFish服务器作为本地主机服务器


请帮忙

不是JavaMail,我使用它作为SendGrid帐户,而是一些旧的apache http代码,可能会帮助您进行调试。我不知道它是否仍然有效,但应该有效。注意端口差异

    SimpleSMTPHeader header;
    AuthenticatingSMTPClient client;
    server = "smtp.gmail.com";

    client = new AuthenticatingSMTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(
                                          new PrintWriter(System.out), true));

    client.connect(server, 587);
    client.ehlo( client.getLocalAddress().getHostName() );
    client.execTLS();

    if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
    {
        client.disconnect();
        System.err.println("SMTP server refused connection.");
        System.exit(1);
    }

    client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN,  "gmail-email@gmail.com", "password");


    sender = "sender@email.com";
    subject = "A subject";

    BufferedReader reader = new BufferedReader( new FileReader( "html/DocumentReport.html") );
    client.mail("<email@email.com>");
    String receipient = "recipient@email.com;

    client.addRecipient(receipient);

    writer = client.sendMessageData();

    header = new SimpleSMTPHeader(sender, receipient, subject);
    header.addHeaderField("Mime-Version", "1.0");
    header.addHeaderField("Content-Type", "text/html; charset=\"ISO-8859-1\"");
    header.addHeaderField("Content-Transfer-Encoding", "7bit");


    writer.write(header.toString());
    try {
        Util.copyReader(reader, writer);
    } finally {
        reader.close();             
    }

    writer.close();

    boolean completed = client.completePendingCommand();

    client.logout();

    client.disconnect();

最后,我得到它现在我可以发送电子邮件。感谢@BillShanon、@KarlNicholas和Yashashvi Kaushik对您的支持。 下面是我所提到的JavaMail Api版本的代码

Properties props= System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");

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

    Authenticator authenticate= new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(username, password);
            }
            };
    @SuppressWarnings("deprecation")
    Session mailSession= Session.getInstance(props, authenticate);
    mailSession.setDebug(true);


    Message mailMessage= new MimeMessage(mailSession);


    try {
        mailMessage.setFrom(new InternetAddress(mailFrom));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        mailMessage.setSubject(subject);
        mailMessage.setText(msg);

        Transport trans= mailSession.getTransport("smtp");
        trans.connect("smtp.gmail.com", username, password);
        Transport.send(mailMessage);

    } catch (Exception ex) {
        Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
    }

除此之外,我还禁用了我的Avast防病毒防火墙,因为这不允许我的程序与我的邮件id连接。希望,这对其他同样存在此问题的编码人员也适用。

修复这些问题,遵循这些问题,如果仍然不起作用,请尝试这些问题。因此,它将停止尝试连接?@BillShannon感谢您的回复,我删除了常见的错误。但是这一次我得到了这个消息:javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:465@KarlNicholas不知道长官什么都没发生,日志长时间保持不变,那时不能连接。你的代码包含几个。请在您的帖子中更新代码,以便下一个复制您代码的人能够获得正确的代码。谢谢