30

我正在使用 javax.mail 用 Ja​​va 发送邮件。现在我的项目概念的一部分发生了变化,我必须在没有身份验证的情况下发送邮件。我将不得不更改我的 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,但我还应该更改什么?

4

2 回答 2

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

我想,这就够了。

于 2013-10-01T12:12:38.607 回答
-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);

}
于 2020-02-13T13:52:11.617 回答