[英]How to save sent items mail using the Java mail API?
How do I save sent items in mail using the Javamail API? 如何使用Javamail API在邮件中保存已发送邮件?
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthenticationgetPasswordAuthentication() {
return new PasswordAuthentication("user id","pwd");
}
});
Message message = new MimeMessage(session);
message.setHeader("X-Priority","1");
message.setFrom(new InternetAddress("user id"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(Toaddress));
message.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
message.setSubject("Subject");
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("test mail");
Transport.send(message);
使用Gmail,您无需将其保存在已发送的文件夹中,Gmail会自动为您执行此操作
here is the solution 这是解决方案
/*
* This is the code for read the sent mails from your mail account.
* Requirements:
* JDK 1.5 and above
* Jar:mail.jar
*
*/
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Flags.Flag;
import javax.mail.search.FlagTerm;
public class ReadSentMail
{
Folder sent;
//Constructor of the class.
public ReadSentMail()
{
/* Set the mail properties */
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com","<email-id>", "<password>");
/* Mention the folder name which you want to read. */
sent = store.getFolder("[Gmail]/Sent Mail");
System.out.println("No of Sent Messages : " +sent.getMessageCount());
/*Open the inbox using store.*/
sent.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Sent Mails*/
Message messages[] = sent.search(new FlagTerm(new Flags(Flag.SEEN), true));
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
sent.fetch(messages, fp);
try
{
printAllMessages(messages);
sent.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (NoSuchProviderException e)
{
e.printStackTrace();
System.exit(1);
}
catch (MessagingException e)
{
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception
{
for (int i = msgs.length-1; i > 0; i--)
{
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
/* Print the envelope(FromAddress,ReceivedDate,Subject) */
public void printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null)
{
for (int j = 0; j < a.length; j++)
{
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null)
{
for (int j = 0; j < a.length; j++)
{
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
System.out.println("Received Date : " + receivedDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg)
{
try
{
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++)
{
readMessageToFile(mp.getBodyPart(i));
}
}
catch (Exception ex)
{
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public void readMessageToFile(Part p) throws Exception
{
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream))
{
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1)
{
System.out.write(c);
}
}
public static void main(String args[])
{
new ReadSentMail();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.