www.fatihkabakci.com

Personal Website and Computer Science TUR EN

JavaMail API

Last update: 6/18/2015 8:22:00 PM

Yazan:Fatih KABAKCI

JavaMail API, J2EE ve Java uygulamalarında internet mail işlemlerinin kullanılması amacıyla tasarlanan bir kütüphanedir ve kullanılabilir JAR dosyası oracle üzerinden indirilebilir. JAR dosyanızı projenize ekledikten sonra, basitçe mail işlemlerini yapabilir hale gelirsiniz. Mail gönderme, mail alma, maile cevap verme, mail iletme, mail arama, mail silme gibi internet üzerinde sıklıkla yaptığınız işlemleri JavaMail API ile programatik olarak kolaylıkla kullanabilirsiniz. Bu API en çok J2EE uygulamalarında önem arz eder. Bir web uygulaması üzerinden bir şirketin müşterilerine toplu mail gönderme işlemlerinde, e-ticaret uygulamalarındaki sipariş sistemlerinde mail işlemleri en çok kullanılan haberleşme araçlarından biridir. Bu yazıda her bir mail işleminin nasıl yapıldığı ayrı ayrı gösterilmektedir. Ancak yazının sonunda açıklanan JavaMailUA sınıfı ve barındığı örnek ile API' nin anlaşılmasını kolaylaştırmaktadır. İlaveten JavaMailUA sınıfını indirebilir ve projelerinizde referans olarak kullanabilirsiniz.

Mail Gönderme(Send Mail)

JavaMail API ile mail göndermek 3 temel adımdan oluşur. Her bir blok kod ileri ki ayrımlarda verilmektedir. Kodlarda örnek olarak hotmail sunucuları kullanılmıştır.

  • Oturum nesnesi(session object) oluşturmak
  • Mesaj nesnesi(message object) oluşturmak
  • Mesajı göndermek(sending message)

1. Session(oturum) oluşturma

Properties properties = new Properties();
      // for example smtp host is hotmail
      properties.put("mail.smtp.host", "smtp.live.com");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.socketFactory.port", "587");
      properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      properties.put("mail.smtp.port", "587");

Session session = Session.getInstance(props, new Authenticator() {
         protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
         }
      });

2. Message(mesaj) oluşturma

MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(userName));
      message.setSubject(subject);
      for (int i = 0; i < to.length; i++)
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      for (int i = 0; i < cc.length; i++)
         message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
      for (int i = 0; i < bcc.length; i++)
         message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));

Multipart multiPart = new MimeMultipart();

      if (attachments != null) {
         // external source part n
         for (String attachment : attachments) {
            DataSource source = new FileDataSource(attachment);
            BodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setDataHandler(new DataHandler(source));
            attachmentPart.setFileName(attachment);
            multiPart.addBodyPart(attachmentPart);
         }
         // text part
         BodyPart textPart = new MimeBodyPart();
         textPart.setText(text);
         multiPart.addBodyPart(textPart);
         // set all contents
         message.setContent(multiPart);
      }
      else {
         message.setContent(text, "text/plain");
      }

3. Mesajı gönderme

Transport.send(msg);

Mail Alma(Receive Mail)

Mail sunucusundan mail okumak için POP veya IMAP protokollerini kullanabilirsiniz. IMAP iki yollu senkronizasyon sağlamasıyla POP' dan farklılık gösterir. Mail mesajlarının işaretlenmesi, farklı klasörlere taşınması IMAP protokolü ile kullanılır. Aşağıda her iki protokol içinde hotmail sunucu ayarları verilmektedir.

Mail alma işlemleri 4 temel adımda gerçekleştirilir.

  • Protokol belirlemek
  • Session object oluşturmak
  • Store object oluşturmak
  • Mesajı elde etmek

1. Protokol belirlemek

Properties properties = new Properties();

// For POP Message
properties.setProperty("mail.pop3s.port", "995");

// For IMAP Message
properties.setProperty("mail.host", "imap-mail.outlook.com");
properties.setProperty("mail.port", "993");
properties.setProperty("mail.transport.protocol", "imaps");

2. Session object oluşturma

Session session = Session.getInstance(props, new Authenticator() {
         protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
         }
      });
      return session;

3. Store object oluşturma

Session session = this.getSession(properties);
// For IMAP
Store store = session.getStore("imaps");
store.connect("imap-mail.outlook.com", userName, password);
// FOR POP3
Store store = session.getStore("pop3s");
store.connect("pop3.live.com", userName, password);

Folder box = this.store.getFolder(folder);
box.open(openType);
Message []message = box.getMessages();

Mail Silme(Delete Mail)

Bir maili silmek için öncelikle ilgili maili elde etmek gerekmektedir. Ardından bir maili silmek için basitçe Flag mekanizmaları arasında bulunan DELETED bayrağını kullanabilirsiniz. Mesajı bir önceki ayrımdaki gibi elde ettikten sonra silebilirsiniz.

message.setFlag(Flag.DELETED, true);
box.close(true);
store.close();

Maile Cevap Verme(Reply to Mail)

Gelen bir maile cevap vermek için öncelikle mail elde edilmeli ve bir oturum oluşturulmalıdır. Ardından gelen mailin nesnesi ile reply() metodu çağrılarak yeni bir mesaj oluşturulur. Bu mesaj ise yaratılan oturum nesnesi üzerinden gönderilir. Aşağıda bu işlemlerin yapıldığı kod bloğu verilmektedir.

      Session session = Session.getInstance(properties);
      Message replyMessage = new MimeMessage(session);
      replyMessage = message.reply(replyAll);
      replyMessage.setFrom(new InternetAddress(userName));
      replyMessage.setReplyTo(message.getReplyTo());
      replyMessage = setContent(replyMessage, text, attachments);

      Transport t = session.getTransport("smtp");
      try {
         t.connect(userName, password);
         t.sendMessage(replyMessage, replyMessage.getAllRecipients());
      }
      finally {
         t.close();
      }

Mail İletme(Forward Mail)

Mail iletme işlemide benzer şekilde oturum nesnesi ve mesaj oluşturularak yapılır. Fark ise, yeni mesajın konusunda Fwd belirtecinin olması ve iletilen mesajın içeriğinin yeni mesajda saklanması gerçeğidir. Bu işlemlerin yapıldığı kod bloğu aşağıda verilmektedir.

Session session = this.getSession(properties);
      Message forward = new MimeMessage(session);
      forward.setSubject("Fwd: " + subject);
      forward.setFrom(new InternetAddress(from));
      for (int i = 0; i < to.length; i++)
         forward.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));

      MimeBodyPart part1 = new MimeBodyPart();
      part1.setContent(message, "message/rfc822");
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(part1);
      // set content
      forward.setContent(multipart);
      forward.saveChanges();

      // Send message
      Transport.send(forward);

Mail Arama(Search Mail)

Mail arama işlemi için AND, OR gibi çeşitli arama kriterleri kullanılır. Aşağıdaki kod bloğunda, bir mailin kimden geldiği ve konusuna göre arama işleminin nasıl yapıldığı gösterilmektedir.

SubjectTerm subjectCriteria = new SubjectTerm(subject);
FromStringTerm fromCriteria = new FromStringTerm(from);
SearchTerm searchCriteria = new AndTerm(subjectCriteria, fromCriteria);
Message []messages = box.search(searchCriteria);

JavaMail API metotları ve sınıflarının nasıl kullanıldığını daha iyi anlamak adına, aşağıda JavaMailUA adlı sınıf örneği verilmektedir. Bu sınıf Java mail kullanıcı temsilcisi anlamına gelen Java mail user agent ifadesi ile tanımlanmıştır. JavaMailUA sınıfında mesaj oluşturmak, mesaj göndermek, mesaj okumak, mesaj iletmek, mesaj silmek, mesaja cevap vermek ve mesaj üzerinde işaretleme, arama gibi çeşitli işlemlerin yapıldığı pratik metotlar bulunmaktadır.

JavaMailUA sınıfı ayrıca bir main() metodu da içermektedir. main() metodu içerisinde örnek olarak 4 kişi arasında bir mail akışı yaratılmaktadır. Örnekte Bob, Alice, John ve Dennis adında 4 mail kullanıcısı bulunmaktadır. Aşağıda örnek mail akışının senaryosu verilmektedir.

Örnek

1. Bob Alice' e aşağıdaki özelliklerde bir mail gönderir.

.
 *    Subject: Hello
 *    Content: Hello Alice !
 *    Attachment: Bob photo
 *    CC: John

2. Alice Bob' dan aldığı mesajı okur ve cevaplar. Cevapladığı mesaj aşağıdaki özellikleri içerir.

 *    Subject: Re: Hello
 *    Content: Hello Bob !
 *    Attachment: Alice photo
 *    CC: John

3. Bob Alice' den aldığı mesajı okur ve okundu olarak işaretler.

4. John Alice' den CC - Carbon Copy sayesinde aldığı maili okur ve Dennise' e iletir.

 *    Subject: Fwd: Re: Hello
 *
 *    -------------------------------
 *    Content: Hello Bob !
 *    Attachment: Alice photo
 *    CC: John

5. Dennis John tarafından gönderilen maili okur ve onu siler.

Yukarıdaki senaryonun uygulandığı örnek JavaMailUA sınıfı aşağıda verilmektedir. Bu sınıf geliştirilmeye açık olarak, ayrıca tüm temel mail işlemlerini yapacak yetenektedir.

Java Mail User Agent - JavaMailUA Class

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.AndTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;

/**
 * Java Mail User Agent class
 * 
 * Bob, Alice, John and Dennis are sample users.
 * 
 * SCENARIO:
 * 
 * 1. Bob sends an e-mail to Alice.
 * 
 *    This e-mail includes
 *    
 *    Subject: Hello
 *    Content: Hello Alice !
 *    Attachment: Bob photo
 *    CC: John
 *    
 * 2. Alice reads e-mail sent by Bob and replies it.
 * 
 *    This e-mail includes
 *    
 *    Subject: Re: Hello
 *    Content: Hello Bob !
 *    Attachment: Alice photo
 *    CC: John
 *    
 * 3. Bob reads e-mail sent by Alice and marks as read.
 * 
 * 4. John reads e-mail sent by Alice and forwards to Dennis.
 * 
 *    This e-mail includes
 *    
 *    Subject: Fwd: Hello
 * 
 * 5. Dennis reads e-mail sent by John and deletes it.
 * 
 * This class sends e-mail using JavaMail API.
 * 
 * Mail settings for Hotmail - Outlook
 * 
 * The POP settings must be enabled in your e-mail account.
 * 
 * IMAP
 * Server: imap-mail.outlook.com
 * SSL: true-implicit
 * Port: 993 (default)
 *
 * POP3
 * Server: pop3.live.com
 * SSL: true-implicit
 * Port: 995 (default)
 *
 * SMTP   
 * Server: smtp.live.com
 * SSL: true-explicit
 * Port: 587 (default)
 * 
 * @author www.fatihkabakci.com
 */
public final class JavaMailUA {
   private String userName;
   private String password;
   private Store  store;
   private Folder box;

   /**
    * @param userName
    * @param password
    */
   public void login(String userName, String password) {
      this.userName = userName;
      this.password = password;
   }

   /**
    * @param to
    * @param cc
    * @param bcc
    * @param subject
    * @param text
    * @param attachment
    * @return
    * @throws MessagingException
    */
   public Message createMessage(String[] to, String[] cc, String[] bcc, String subject, String text, String[] attachment)
                                                                                                                         throws MessagingException {
      Properties properties = new Properties();
      // for example smtp host is hotmail
      properties.put("mail.smtp.host", "smtp.live.com");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.socketFactory.port", "587");
      properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      properties.put("mail.smtp.port", "587");

      Session session = this.getSession(properties);

      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(userName));
      message.setSubject(subject);
      for (int i = 0; i < to.length; i++)
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      for (int i = 0; i < cc.length; i++)
         message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
      for (int i = 0; i < bcc.length; i++)
         message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));

      Message msg = setContent(message, text, attachment);

      return msg;
   }

   /**
    * @param to
    * @param cc
    * @param bcc
    * @param subject
    * @param text
    * @throws AddressException
    * @throws MessagingException
    * @throws IOException
    */
   public void send(String to, String cc, String bcc, String subject, String text) throws AddressException, MessagingException,
                                                                                  IOException {
      this.send(new String[]{to}, new String[]{cc}, new String[]{bcc}, subject, text, new String[]{});
   }

   /**
    * @param to
    * @param cc
    * @param bcc
    * @param subject
    * @param text
    * @param attachment
    * @throws AddressException
    * @throws MessagingException
    * @throws IOException
    */
   public void send(String[] to, String[] cc, String[] bcc, String subject, String text, String[] attachment)
                                                                                                             throws AddressException,
                                                                                                             MessagingException,
                                                                                                             IOException {
      Properties properties = new Properties();
      // for example smtp host is hotmail
      properties.put("mail.smtp.host", "smtp.live.com");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.socketFactory.port", "587");
      properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      properties.put("mail.smtp.port", "587");

      Session session = this.getSession(properties);

      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(userName));
      message.setSubject(subject);
      for (int i = 0; i < to.length; i++)
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      for (int i = 0; i < cc.length; i++)
         message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
      for (int i = 0; i < bcc.length; i++)
         message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));

      Message msg = setContent(message, text, attachment);
      Transport.send(msg);
      System.out.println("The mail was successfully sent");
   }

   /**
    * @param message
    * @param to
    * @param cc
    * @param bcc
    * @throws AddressException
    * @throws MessagingException
    * @throws IOException
    */
   public void send(Message message, String[] to, String[] cc, String[] bcc) throws AddressException, MessagingException,
                                                                            IOException {
      for (int i = 0; i < to.length; i++)
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      for (int i = 0; i < cc.length; i++)
         message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
      for (int i = 0; i < bcc.length; i++)
         message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));

      Transport.send(message);
      System.out.println("The mail was successfully sent");
   }

   /**
    * @param message
    * @throws Exception
    */
   public void read(Message message) throws Exception {
      String from = InternetAddress.toString(message.getFrom());
      String replyTo = InternetAddress.toString(message.getReplyTo());
      String subject = message.getSubject();
      Date sentDate = message.getSentDate();
      Date receivedDate = message.getReceivedDate();

      System.out.println("Message Start");
      System.out.println();
      System.out.println("From: " + from);
      System.out.println("Reply To: " + replyTo);
      System.out.println("Subject : " + subject);
      System.out.println("Sent Date: " + sentDate);
      System.out.println("Received Date : " + receivedDate);
      System.out.println();

      Multipart mp = (Multipart) message.getContent();
      for (int i = 0; i < mp.getCount(); i++) {
         BodyPart body = mp.getBodyPart(i);
         System.out.println("Content " + (i + 1));
         this.printPart(body);
      }
      System.out.println("Message End");
   }

   /**
    * @param message
    * @throws MessagingException
    * @throws IOException
    */
   public void delete(Message message) throws MessagingException, IOException {
      message.setFlag(Flag.DELETED, true);
      this.box.close(true);
      this.store.close();
      System.out.println("[INFO]: Message deleted");
   }

   /**
    * @param message
    * @throws MessagingException
    * @throws IOException
    */
   public void markAsRead(Message message) throws MessagingException, IOException {
      message.setFlag(Flag.SEEN, true);
      this.box.close(true);
      this.store.close();
      System.out.println("[INFO]: Message marked as read");
   }

   /**
    * @param message
    * @param text
    * @param attachments
    * @param replyAll
    * @throws MessagingException
    * @throws IOException
    */
   public void reply(Message message, String text, String attachments[], boolean replyAll) throws MessagingException, IOException {
      Properties properties = new Properties();
      properties.put("mail.smtp.host", "smtp.live.com");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.socketFactory.port", "587");
      properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      properties.put("mail.smtp.port", "587");

      Session session = Session.getInstance(properties);
      Message replyMessage = new MimeMessage(session);
      replyMessage = message.reply(replyAll);
      replyMessage.setFrom(new InternetAddress(userName));
      replyMessage.setReplyTo(message.getReplyTo());
      replyMessage = setContent(replyMessage, text, attachments);

      Transport t = session.getTransport("smtp");
      try {
         t.connect(userName, password);
         t.sendMessage(replyMessage, replyMessage.getAllRecipients());
      }
      finally {
         t.close();
      }
      System.out.println("[INFO]: Message Replied");
      box.close(true);
      store.close();
   }

   /**
    * @param message
    * @param to
    * @throws MessagingException
    * @throws IOException
    */
   public void forward(Message message, String to) throws MessagingException, IOException {
      this.forward(message, new String[]{to});
   }

   /**
    * @param message
    * @param to
    * @throws MessagingException
    * @throws IOException
    */
   public void forward(Message message, String[] to) throws MessagingException, IOException {
      Properties properties = new Properties();
      properties.put("mail.smtp.host", "smtp.live.com");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.socketFactory.port", "587");
      properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      properties.put("mail.smtp.port", "587");

      String from = InternetAddress.toString(message.getFrom());
      String subject = message.getSubject();

      Session session = this.getSession(properties);
      Message forward = new MimeMessage(session);
      forward.setSubject("Fwd: " + subject);
      forward.setFrom(new InternetAddress(from));
      for (int i = 0; i < to.length; i++)
         forward.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));

      MimeBodyPart part1 = new MimeBodyPart();
      part1.setContent(message, "message/rfc822");
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(part1);
      // set content
      forward.setContent(multipart);
      forward.saveChanges();

      // Send message
      Transport.send(forward);
      System.out.println("[INFO]: Message forwarded");
   }

   /**
    * @param folder
    * @param subject
    * @param from
    * @param openType
    * @return
    * @throws MessagingException
    */
   public Message[] getPopMessage(String folder, String subject, String from, int openType) throws MessagingException {
      Properties properties = new Properties();
      properties.setProperty("mail.pop3s.port", "995");

      Session session = Session.getDefaultInstance(properties);
      this.store = session.getStore("pop3s");
      this.store.connect("pop3.live.com", userName, password);

      this.box = store.getFolder(folder);
      this.box.open(openType);

      SubjectTerm subjectCriteria = new SubjectTerm(subject);
      FromStringTerm fromCriteria = new FromStringTerm(from);
      SearchTerm searchCriteria = new AndTerm(subjectCriteria, fromCriteria);
      return this.box.search(searchCriteria);
   }

   /**
    * @param folder
    * @param subject
    * @param from
    * @param openType
    * @return
    * @throws MessagingException
    */
   public Message[] getImapMessage(String folder, String subject, String from, int openType) throws MessagingException {
      Properties properties = new Properties();
      properties.setProperty("mail.host", "imap-mail.outlook.com");
      properties.setProperty("mail.port", "993");
      properties.setProperty("mail.transport.protocol", "imaps");

      Session session = this.getSession(properties);
      this.store = session.getStore("imaps");
      this.store.connect("imap-mail.outlook.com", userName, password);

      this.box = this.store.getFolder(folder);
      this.box.open(openType);

      SubjectTerm subjectCriteria = new SubjectTerm(subject);
      FromStringTerm fromCriteria = new FromStringTerm(from);
      SearchTerm searchCriteria = new AndTerm(subjectCriteria, fromCriteria);
      return this.box.search(searchCriteria);
   }

   /**
    * @param part
    * @throws Exception
    */
   private void printPart(Part part) throws Exception {
      if (part.isMimeType("text/plain")) {
         System.out.println((String) part.getContent());
      }
      // check if the content has attachment
      else if (part.isMimeType("multipart/*")) {
         Multipart mp = (Multipart) part.getContent();
         for (int i = 0; i < mp.getCount(); i++)
            printPart(mp.getBodyPart(i));
      }
      // check if the content is a nested message
      else if (part.isMimeType("message/rfc822")) {
         printPart((Part) part.getContent());
      }
      else {
         System.out.println(part.getContentType());
      }
   }

   /**
    * @param props
    * @return
    */
   private Session getSession(Properties props) {
      Session session = Session.getInstance(props, new Authenticator() {
         protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
         }
      });
      return session;
   }

   /**
    * @param message
    * @param text
    * @param attachments
    * @return
    * @throws MessagingException
    */
   private Message setContent(Message message, String text, String[] attachments) throws MessagingException {
      Multipart multiPart = new MimeMultipart();

      if (attachments != null) {
         // external source part n
         for (String attachment : attachments) {
            DataSource source = new FileDataSource(attachment);
            BodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setDataHandler(new DataHandler(source));
            attachmentPart.setFileName(attachment);
            multiPart.addBodyPart(attachmentPart);
         }
         // text part
         BodyPart textPart = new MimeBodyPart();
         textPart.setText(text);
         multiPart.addBodyPart(textPart);
         // set all contents
         message.setContent(multiPart);
      }
      else {
         message.setContent(text, "text/plain");
      }
      return message;
   }

   public static void main(String[] args) throws Exception {
      final String bobUserName = "bob@web.com";
      final String bobPassword = "1234";
      final String aliceUserName = "alice@web.com";
      final String alicePassword = "1234";
      final String johnUserName = "john@web.com";
      final String johnPassword = "1234";
      final String dennisUserName = "dennis@web.com";
      final String dennisPassword = "1234";

      final String to[] = {aliceUserName};
      final String cc[] = {johnUserName};
      final String bcc[] = {};
      final String attachments1[] = {"C:\\Users\\fkabakci\\Desktop\\bob.jpg"};
      final String attachments2[] = {"C:\\Users\\fkabakci\\Desktop\\alice.jpg"};
      final String INBOX = "INBOX";

      JavaMailUA bob = new JavaMailUA();
      bob.login(bobUserName, bobPassword);
      bob.send(to, cc, bcc, "Hello", "Hello Alice !", attachments1);

      JavaMailUA alice = new JavaMailUA();
      alice.login(aliceUserName, alicePassword);
      Message message = alice.getPopMessage(INBOX, "Hello", bobUserName, Folder.READ_WRITE)[0];
      alice.read(message);
      alice.reply(message, "Hello Bob !", attachments2, true);

      message = bob.getImapMessage(INBOX, "Hello", aliceUserName, Folder.READ_WRITE)[0];
      bob.read(message);
      bob.markAsRead(message);

      JavaMailUA john = new JavaMailUA();
      john.login(johnUserName, johnPassword);
      message = john.getPopMessage(INBOX, "Hello", aliceUserName, Folder.READ_WRITE)[0];
      john.forward(message, dennisUserName);

      JavaMailUA dennis = new JavaMailUA();
      dennis.login(dennisUserName, dennisPassword);
      message = dennis.getPopMessage(INBOX, "Hello", johnUserName, Folder.READ_WRITE)[0];
      dennis.read(message);
      dennis.delete(message);
   }
}

JavaMailUA sınıfını buradan indirebilirsiniz.

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.