JavaMail Use of small notes

Source: Internet
Author: User
Tags static class throwable

/**
*
*/
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.