4 Haziran 2018 Pazartesi

JavaMail MimeMessage Sınıfı

Giriş
Bu sınıfı kullanmak için şu satır dahil edilir. Bu sınıf .Net'teki MailMessage sınıfına benzer. Message soyut sınıfından kalıtır.
import javax.mail.internet.MimeMessage;
Bu nesne aslında dış zarf gibi düşünülebilir. İçinde from,to,subject ve eklentileri taşır.
Kullanım
Nesne doldurulduktan sonra Transport nesnesi ile gönderilir.
Örnek
Şöyle yaparız.
MimeMessage message = new MimeMessage(session);    
...
//send message  
Transport.send(message);    
Örnek
Eklenti olmadan göndermek için şöyle yaparız.
String from = ...;
String password= ..;
String subject = ...;
String message = ...;
String to =...;


Session session = ...;

Message msg = new MimeMessage(session);
msg.setText(emailBody);
msg.setSubject(emailSubject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail));
Transport.send(msg);

constructor
Şöyle yaparız.
Session session = ...;
MimeMessage mimeMsg = new MimeMessage (session);
addRecipient metodu
Şöyle yaparız.
final String to="xxxx4@gmail.com";
mimeMsg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));   
saveChanges metodu
Şöyle yaparız.
mimeMsg.saveChanges ();
setContent - Multipart
MimeMultipart ile hem html hem de eklenti göndermek isteyelim MimeMultipart nesnesine addBodyPart() çağrısı ile ekleriz. Daha sonra bu nesneyi setContent() çağrısı ile MimeMessage nesnesin ekleriz.

Örnek
Şöyle yaparız.
Multipart multipart = new MimeMultipart();
multipart.addBodyPart (mimeBodyPart);
multipart.addBodyPart (attachPart);

mimeMsg.setContent (multipart);
Örnek
Şöyle yaparız.
// Your mail has 2 parts, the BODY(html) and the EMBEDDED image
MimeMultipart multipart = new MimeMultipart("related");

// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");

// Add it
multipart.addBodyPart(messageBodyPart);

// Second part (EMBEDDED image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("/path/to/your/image/tr.png");

messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");

// add the image to the multipart
multipart.addBodyPart(messageBodyPart);

// put everything together
message.setContent(multipart);

// Send message
Transport.send(message);
setFrom metodu
Şöyle yaparız.
String from1 = "me@mycompany.com";
mimeMsg.setFrom (new InternetAddress(from));
setRecipient metodu
To için şöyle yaparız.
String to = "to@abc.com";
mimeMsg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
Cc için şöyle yaparız.
String ccAddress = ...;
mimeMsg.setRecipients (Message.RecipientType.CC, ccAddress);
setSubject metodu
Şöyle yaparız.
String subject = ...;
mimeMsg.setSubject (subject);
setText metodu
Şöyle yaparız.
String body = ...;
mimeMsg.setText (body);
setSentDate metodu
Şöyle yaparız.
mimeMsg.setSentDate (new Date());
Diğer Notlar
Bilgisayardaki kurulu e-posta uygulamasını açmak için awt.Desktop sınıfını kullanırız. Desktop yazısına bakabilirsiniz.




1 Haziran 2018 Cuma

@Documented Anotasyonu

Giriş
Açıklaması şöyle.
Indicates that annotations with a type are to be documented by javadoc and similar tools by default. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.
Kendi anostasyonlarımızın javadoc ile üretilen html'de çıkmasını sağlar. Açıklaması şöyle.
This annotation can be applied to other annotations. It means that the annotated elements will be documented using the Javadoc tool.

Örnek
Şöyle yaparız
import java.lang.annotation.Documented;

@Documented
public @interface MyAnnotation {
}
Örnek
Şöyle yaparız.
@Documented
@Retention(RUNTIME)
@Target({ANNOTATION_TYPE, TYPE})
@Constraint(validatedBy = SecondaryValidator.class)
public @interface ValidSecondary {
    String message() default "Invalid secondary";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}