Java Mail Pertanyaan dan Jawaban Wawancara
Question: Sample code for replying to messages using JavaMail.Answer: The Message class have a reply() method to configure a new Message with the proper recipient and subject, adding "Re: " if not already there. This does not add any content to the message, reply() method have a boolean parameter indicating whether to reply to only the sender (false) or reply to all (true).MimeMessage reply = (MimeMessage)message.reply(false);reply.setFrom(new InternetAddress("president@whitehouse.gov"));reply.setText("Thanks");Transport.send(reply); To configure the reply-to address when sending a message, use the setReplyTo() method. package com.withoutbook.common; import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class ReplyMail { public static void main(String args[]) throws Exception { Date date = null; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", "192.168.10.110"); Session session = Session.getDefaultInstance(properties); Store store = session.getStore("pop3"); store.connect("192.168.10.110", "arindam", "arindam"); Folder folder = store.getFolder("inbox"); if (!folder.exists()) { System.out.println("inbox not found"); System.exit(0); } folder.open(Folder.READ_ONLY); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Message[] message = folder.getMessages(); if (message.length != 0) { System.out.println("no. From ttSubject ttDate"); for (int i = 0, n = message.length; i < n; i++) { date = message[i].getSentDate(); System.out.println(" " + (i + 1) + ": " + message[i].getFrom()[0] + "t" + message[i].getSubject() + " t" + date.getDate() + "/" + date.getMonth() + "/" + (date.getYear() + 1900)); System.out.print("Do you want to reply [y/n] : "); String ans = reader.readLine(); if ("Y".equals(ans) || "y".equals(ans)) { // Create a reply message MimeMessage reply = (MimeMessage) message[i].reply(false); // Set the from field reply.setFrom(message[i].getFrom()[0]); // Create the reply content // Create the reply content, copying over the original if text MimeMessage orig = (MimeMessage) message[i]; StringBuffer buffer = new StringBuffer("Thanksnn"); if (orig.isMimeType("text/plain")) { String content = (String) orig.getContent(); StringReader contentReader = new StringReader(content); BufferedReader br = new BufferedReader(contentReader); String contentLine; while ((contentLine = br.readLine()) != null) { buffer.append("> "); buffer.append(contentLine); buffer.append("rn"); } } // Set the content reply.setText(buffer.toString()); // Send the message Transport.send(reply); } else if ("n".equals(ans)) { break; } } } else { System.out.println("There is no msg...."); } } } |
Simpan untuk Revisi
Bookmark item ini, tandai sebagai sulit, atau masukkan ke dalam set revisi.
Masuk untuk menyimpan bookmark, pertanyaan sulit, dan set revisi.
Apakah ini membantu? Ya Tidak
Most helpful rated by users:
- What is JavaMail?
- Explain POP, SMTP and IMAP protocols.
- Discuss about JavaMail.
- Explain the structure of Javamail API
- What are the advantages of JavaMail?