在 javax.mail 中发送邮件而不进行身份验证

2022-09-01 09:57:45

我正在使用javax.mail在Java中发送邮件。现在,我的项目概念的一部分发生了变化,我必须在没有身份验证的情况下发送邮件。我将不得不改变我的 createSession() 方法:

private void createSession() {
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", server);
    properties.put("mail.smtp.port", port);

    session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
}

很明显,我应该更改为 ,但是我还应该更改什么呢?mail.smtp.authfalse


答案 1
private void createSession() {
    properties.put("mail.smtp.auth", "false");
     //Put below to false, if no https is needed
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", server);
    properties.put("mail.smtp.port", port);

    session = Session.getInstance(properties);
}

我想,这就足够了。


答案 2
private void createSession() {
  properties.put("mail.smtp.auth",false);
  properties.put("mail.smtp.starttls.enable","true");
  properties.put("mail.smtp.port","587");
  properties.put("mail.smtp.host","smtp.gmail.com");
  properties.put("mail.smtp.username","username(emailid")");
  properties.put("mail.smtp.password","password(mail password)");

  Session session=Session.getDefaultInstance(properties,null);

}

推荐