Outgoing items
Package com.sun.mail;
Import java.io.UnsupportedEncodingException;
Import java.util.Properties;
Import Javax.mail.Authenticator;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
public class SendMail2 {
public static void Main (string[] args) throws Unsupportedencodingexception {
Boolean Isssl = true;
String host = "smtp.163.com";
int port = 465;
String from = "Sender address";
String to = "Recipient address";
Boolean Isauth = true;
Final String username = "Sender address";
Final String Password = "Sender address password";
Properties Props = new properties ();
Props.put ("mail.smtp.ssl.enable", Isssl);
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.port", port);
Props.put ("Mail.smtp.auth", Isauth);
Session session = Session.getdefaultinstance (props, new Authenticator () {
@Override
Protected Passwordauthentication getpasswordauthentication () {
return new passwordauthentication (username, password);
}
});
try {
Message message = new MimeMessage (session);
Message.setfrom (New InternetAddress (from));
Message.addrecipient (Message.RecipientType.TO, New internetaddress (to));
Message.setsubject ("Test Mail");
Message.settext ("bingo!");
Transport.send (message);
} catch (Addressexception e) {
E.printstacktrace ();
} catch (Messagingexception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Send complete! ");
}
}
Receive items
Package com.sun.mail;
Import java.io.IOException;
Import Java.util.Date;
Import java.util.Properties;
Import Javax.mail.Folder;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.NoSuchProviderException;
Import javax.mail.Session;
Import Javax.mail.Store;
public class FetchMail2 {
public static void Main (string[] args) throws IOException {
String protocol = "POP3";
Boolean Isssl = true;
String host = "pop.qq.com";
int port = 995;
String username = "Recipient Address";
String Password = "recipient address password";//qq now requires an authorization code
Properties Props = new properties ();
Props.put ("mail.pop3.ssl.enable", Isssl);
Props.put ("Mail.pop3.host", host);
Props.put ("Mail.pop3.port", port);
Session session = Session.getdefaultinstance (props);
Store store = null;
Folder folder = null;
try {
store = session.getstore (protocol);
Store.connect (username, password);
folder = Store.getfolder ("INBOX");
Folder.open (folder.read_only);
int size = Folder.getmessagecount ();
Message message = folder.getmessage (size);
String from = Message.getfrom () [0].tostring ();
String subject = Message.getsubject ();
Date date = Message.getsentdate ();
System.out.println ("From:" + from);
System.out.println ("Subject:" + Subject);
System.out.println ("Content:" +message.getcontent ());
System.out.println ("Date:" + date);
} catch (Nosuchproviderexception e) {
E.printstacktrace ();
} catch (Messagingexception e) {
E.printstacktrace ();
} finally {
try {
if (folder! = null) {
Folder.close (FALSE);
}
if (store! = null) {
Store.close ();
}
} catch (Messagingexception e) {
E.printstacktrace ();
}
}
SYSTEM.OUT.PRINTLN ("Reception complete! ");
}
}
Java Mail send and receive (only English, Chinese required transcoding)