Properties etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Properties etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

28 Şubat 2018 Çarşamba

JavaMail Properties Sınıfı

Giriş
Şu satırı dahil ederiz.
import java.util.Properties;
Örnek
Şu alanlar doldurulur.
mail.port=25

mail.smtp.starttls.enable=true
mail.smtp.auth=true
mail.transport.protocol=smtp
mail.debug=true
mail.smtp.ssl.trust = localhost
mail.host = localhost
mail.username=XXXXXX
mail.password=XXXXXX
Örnek
Şu alanlar doldurulur.
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Auth
Sunucunun bizi doğrulaması için kullanılır. Şöyle yaparız.
props.put("mail.smtp.auth", "true");
Debug
Şöyle yaparız.
props.put("mail.smtp.debug", "true");
Host ve Port
Şöyle yaparız.
props.put("mail.smtp.host", "mail.mydomain.com");
props.put("mail.smtp.port", "587");
SSL
SSL kullanmak için Properties içine verilen mail.smtp.ssl.enable anahtarı true yapılır. SMPT yerine POP3, IMAP gelebilir.

Şöyle yaparız.
props.put("mail.smtp.socketFactory.port", "465");    
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  

TLS
Bağlantı kurulduktan sonra TLS'e geçmek için mail.smtp.starttls.enable anahtarı true yapılır.
props.put("mail.smtp.starttls.enable", "true");
Şu açıklama faydalı.
TLS is compulsory on 587 and any attempt to open a session without TLS yield the SMTP server error response "530 Must issue a STARTTLS command first". Then setting "mail.smtp.starttls.enable" to "true" alone still yield the same error "Could not convert socket to TLS" but now with a clue: "Server is not trusted". Indeed, you must have either a keystore defined in the JVM start properties that would contain a certificate chain ending onto a trusted root certificate, either enforce trust with this extra property: "mail.smtp.ssl.trust" set to the remote host name.


2 Ağustos 2017 Çarşamba

Properties Sınıfı

Giriş
Properties dosyasında ayraç olarak :, = veya boşluk karakteri kullanılabiliyor. Açıklaması şöyle
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character;
...
As an example, each of the following three lines specifies the key "Truth" and the associated element value "Beauty":
Truth = Beauty
Truth:Beauty
Truth :Beauty
Ben ayraç olarak = karakteri kullanılmasını tercih ediyorum. Şöyle yapabiliriz.
CarModel=Prius
CarMake=Toyota
Option1=Transmission
OptionValue1a=Manual
OptionValue1b=Automatic
Option2=Brakes
OptionValue2a=Regular
OptionValue2b=ABS
Properties Java'nın en başından beri var. Map ve HashMap sınıfları geliştirilmeden önce de vardı. Bu yüzden eski HashTable sınıfından kalıtıyor.

constructor
Şöyle yaparız.
Properties props = new Properties ();
getProperty metodu
Şöyle yaparız. Eğer key yoksa null döner.
String carModel = props.getProperty("CarModel");
if(!carModel.equals (null)){...}
load metodu
Açıklaması şöyle
The specified stream remains open after this method returns.
Dolayısıylsa şu kod stream'i kapatmadığı için yanlış.
Properties props = new Properties();
props.load(new FileInputStream(...));
Şöyle yaparız. İş bitince FileInputStream'in kapatılıyor
FileInputStream fis = new FileInputStream(filename);
props.load (fis);
Java 7 ile şöyle yaparız.
try (FileInputStream fis = new FileInputStream("C:/my.ini")) {
  props.load (fis);
}
setProperty metodu
Şöyle yaparız.
String property = ...; String value = ...;
props.setProperty (property, value);
Eğer value değeri içinde ayraç varsa otomatik escape edililir.
properties.setProperty("foo", "bar:baz");
Çıktı olarak şunu alırız.
foo=bar\:baz
store metodu - OutputStream
Şöyle yaparız. İkinci parametre olan comments null verilebilir.
FileOutputStream out = ...;
props.store (out, null);
store metodu - Writer
Şöyle yaparız.
Path path = Paths.get("x.properties");
try (
  // Requires Java 8!
  final Writer writer = Files.newBufferedWriter (path);
  ) {
    properties.store (writer, null);
}