簡體   English   中英

在 javax.mail 中發送郵件而無需身份驗證

[英]Send mail in javax.mail without authentication

我正在使用 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.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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM