JavaMail Example code
The code segment shown in Example 6-2 illustrates how an application component sends a message and saves it to the Sent folder.
Example 6-2 JavaMail application code
//get JavaMail session javax.naming.InitialContext ctx = new javax.naming.InitialContext(); javax.mail.Session mail_session = (javax.mail.Session) ctx.lookup("java:comp/env/mail/MailSession"); //prepare message MimeMessage msg = new MimeMessage(mail_session); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("bob@coldmail.net")); msg.setFrom(new InternetAddress("alice@mail.eedge.com")); msg.setSubject("Important message from eEdge.com"); msg.setText(msg_text); //send message Transport.send(msg); //save message in "Sent" folder Store store = mail_session.getStore(); store.connect(); Folder f = store.getFolder("Sent"); if (!f.exists()) f.create(Folder.HOLDS_MESSAGES); f.appendMessages(new Message[] {msg});