微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javax.mail PasswordAuthentication 未运行

如何解决javax.mail PasswordAuthentication 未运行

我一直在尝试使用 javax.mail 开发 gmail 客户端。一切似乎都没有错误,但即使我输入错误密码,它也不会执行或给出异常。

我在我的 Google 帐户中启用了访问安全性较低的应用。

const AddFile = require('../models/add_file');

const newFile = new AddFile({
    title: req.body.data,pdf: {
      data: fs.readFileSync(path.join(__dirname + '/public/files/' + req.file.filename)),//this line throws error
      contentType: 'application/pdf'
    }
});
newFile.save(...);

终端输出

package com.email; 
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
private String user = "khairinahizar@gmail.com";
private String pass = "kh";

SendEmail(String to,String sub,String msg) {
    Properties prop = new Properties();
    prop.put("mail.smtp.ssl.trust","smtp.gmail.com");
    prop.put("mail.smtp,auth",true);
    prop.put("mail.smtp.starttls.enable",true);
    prop.put("mail.smtp.host","smtp.gmail.com");
    prop.put("mail.smtp.port","587");
    
    Session session = Session.getInstance(prop,new javax.mail.Authenticator() 
    {
        protected javax.mail.PasswordAuthentication getpasswordAuthentication() 
        {
            return new javax.mail.PasswordAuthentication(user,pass);
        }
    });
    try {
        Message message = new MimeMessage(session);
        
        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
        message.setSubject(sub);
        message.setText(msg);
        
        Transport.send(message);
    }
    catch (Exception e) {
        
    }
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。