/**
*
Send mail mainly have properties (send mail various properties server address, port, username password, etc.), Authenticator (verify) These two build a session,message set a message, and then transport class send
*/
Package com.util;
Import java.io.UnsupportedEncodingException;
Import java.util.Properties;
Import javax.mail.Authenticator;
Import javax.mail.Folder;
Import javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.NoSuchProviderException;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import javax.mail.Store;
Import javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import javax.mail.internet.MimeMessage;
Import javax.mail.internet.MimeUtility;
/**
* @author WQF
*
*/
public class Emailsendtools {
Private final static String _email_account = "[EMAIL protected]";
Private final static String _email_password = "XXX";
Private final static String _smtp_host = "smtp.qq.com";
Private final static String _smtp_port = "465";
Private final static String _pop3_host = "pop.qq.com";
Private final static String _pop3_port = "995";
/**
* @param args
*/
public static void main (string[] Args) {
SendEmail (_email_account, "test", "test");
Receivemail ();
}
public static void Receivemail () throws sendemailexception{
try {
Store store = getsession (false). getStore ();
Store.connect ();
Folder folder = Store.getfolder ("INBOX");
Folder.open (folder.read_only);
message[] msgs = folder.getmessages (1,20);
int count = msgs.length;
for (int i = 0; i < count; i++) {
System.out.println ("sender:" + decodetext (msgs[i].getfrom () [0].tostring ())];
System.out.println ("subject:" + msgs[i].getsubject (). toString ());
}
} catch (nosuchproviderexception E) {
throw new sendemailexception ("link message failed" + e.getmessage (), e);
} catch (messagingexception E) {
throw new sendemailexception ("read inbox failed:" + e.getmessage (), e);
} catch (unsupportedencodingexception E) {
throw new Sendemailexception ("message parsing failed:" + e.getmessage (), e);
}
}
private static string Decodetext (string Text)
Throws unsupportedencodingexception{
if (text = = Null)
Return null;
If (text.startswith ("=? GB ") | | Text.startswith ("=?gb") | | Text.startswith ("=? UTF ") | | Text.startswith ("=?utf"))
Text = Mimeutility.decodetext (text);
Else
Text = new String (text.getbytes ("iso8859_1"));
Return text;
}
/**
*
* @param from
* @param to
* @param title
* @param emsg
* @return Boolean
* @throws sendemailexception
*/
public static Boolean SendEmail (string to, string title, string Emsg) throws Sendemailexception {
try {
Session session = GetSession (true);
MimeMessage message = new MimeMessage (session);
Message.setfrom (new internetaddress (_email_account));
Message.addrecipient (Message.RecipientType.TO, New InternetAddress (to));
Message.setsubject (title);
Message.settext (emsg);
Message.savechanges ();
Transport.send (message);
} catch (messagingexception E) {
Throw new Sendemailexception ("message sent failed:" + e.getmessage (), e);
}
Return true;
}
private static Properties Getprop (boolean Send) {
Security.addprovider (new Com.sun.net.ssl.internal.ssl.Provider ());
Properties prop = new properties ();
If (send) {
Prop.setproperty ("mail.transport.protocol", "smtp");
Prop.setproperty ("mail.smtp.host", _smtp_host);
Prop.setproperty ("mail.smtp.auth", "true");
Prop.setproperty ("mail.smtp.port", _smtp_port);
Prop.setproperty ("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Prop.setproperty ("mail.smtp.socketFactory.fallback", "false");
Prop.setproperty ("mail.smtp.socketFactory.port", _smtp_port);
} else {
Prop.setproperty ("mail.store.protocol", "pop3");
Prop.setproperty ("mail.pop3.host", _pop3_host);
Prop.setproperty ("mail.pop3.port", _pop3_port);
Prop.setproperty ("mail.pop3.auth", "true");
Prop.setproperty ("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Prop.setproperty ("mail.pop3.socketFactory.fallback", "false");
Prop.setproperty ("mail.pop3.socketFactory.port", _pop3_port);
}
Return prop;
}
private static Session getsession (boolean Send) {
Session session = Session.getdefaultinstance (getprop (send), getaut ());
Session.setdebug (true);
Return session;
}
private static Authenticator Getaut () {
Authenticator aut = new Myauthenricator (_email_account, _email_password);
Return aut;
}
private static class Sendemailexception extends RuntimeException {
/**
*
*/
Private static final Long Serialversionuid = 1L;
@SuppressWarnings ("unused")
Public Sendemailexception () {
Super ();
}
@SuppressWarnings ("unused")
Public Sendemailexception (String Msg) {
Super (msg);
}
@SuppressWarnings ("unused")
Public Sendemailexception (throwable Cause) {
Super (cause);
}
Public sendemailexception (String message, throwable Cause) {
Super (message, cause);
}
}
/**
* Client program implement authenticator subclass for user authentication
*/
Static Class Myauthenricator extends Authenticator {
String user = null;
String pass = "";
Public myauthenricator (string user, string Pass) {
This.user = user;
This.pass = pass;
}
@Override
Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (user, pass);
}
}
}
JavaMail Use of small notes