在javax.mail中发送邮件而无需身份验证



我正在使用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.auth更改为false,但是我还应该更改什么?

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);
}

我想,这就足够了。

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);
}

相关内容

最新更新