简体   繁体   English

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

[英]Send mail in javax.mail without authentication

I am using javax.mail to send mails in Java.我正在使用 javax.mail 用 Ja​​va 发送邮件。 Now that a part of the concept of my project changed I have to send a mail without authentication.现在我的项目概念的一部分发生了变化,我必须在没有身份验证的情况下发送邮件。 I will have to change my createSession() method:我将不得不更改我的 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);
        }
    });
}

It is rather obvious that I should change mail.smtp.auth to false , but what else should I change?很明显我应该将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);
}

I think, this would suffice.我想,这就够了。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM