Access Hotmail Email by JavaMail

xiaoxiao2021-03-06  88

Liu Dong

(Winter.lau@163.com) Java Free People, October 2004

I believe many people have an account of MSN chat tools, such as abc@hotmail.com, this account is actually a mail address, you can perform normal mail transceiver features, you can access and access the URL http://www.hotmail.com Operate this mailbox for mail delivery. But this mailbox has a limit that it does not provide POP3 or SMTP service, that is, the general mail client tool cannot use this mailbox, although there are now many email tools to support Hotmail mailbox, but if we need it in our In the Java project, the support of Hotmail mailbox is increasing, which often does not know where to start, because Sun's JavaMail itself does not provide support for other protocols other than POP3 / IMAP and SMTP.

But in fact, JavaMail only defines a set of platforms, independent of the mail program framework of the communication protocol, or an interface, which is an optional package as Java. So you can say this: Javamail does not care about the protocol used, whether it is POP3, SMTP, iMap, of course, HTTP protocol used by Hotmail. This is also the case for JavaMail's client. The following figure is the simplest JavaMail structure diagram. The message is sent via TRANSPORT, and through the Store's charge, the implementation of different protocols, the concept of JavaMail is called Provider, Just need to implement your own provider according to the interface defined by JavaMail.

In order to access the Hotmail mailbox through JavaMail, we need to set up a bridge between the JavaMail interface and the access interface provided by the Hotmail service. Before this, we must first learn about the interface protocol used by Hotmail.

Hotmail provides HTTP protocol-based services via address http://services.msn.com/svcs/hotmail/httpmail.asp, which is different from this address of http://www.hotmail.com through the browser, followed by this The address is provided to the user a browser-based access interface for operation of the mailbox, you can of course analyze the page and get the message information by writing the HTTP client program, but do not only achieve complexity, but also each time The procedures have been adjusted after the page style changes, so there is no versatility, it is obviously not feasible. The address http://services.msn.com/svcs/hotmail/httpmail.asp is different, which provides a fixed application interface. When you use your browser to access the address, the form that you can log in is populated:

When you enter the correct username and password, you will then pop up or twice, but the message is different, the page cannot be displayed after the operation is completed, and the error code is 405 (resource is not allowed). This service is therefore not allowed to be accessed through a browser.

In fact, Hotmail uses WebDAV to provide an application programming interface based on HTTP protocol, WebDAV (web distributed creation and version control) has become an important web communication protocol. The problem to be solved by WebDAV is:

1. Rewind protection: HTTP 1.1 cannot ensure that the client can protect resources and can be changed while other clients edit them. With WebDAV, you can lock resources in a variety of ways to let other clients know that you are interested in the resources discussed, or prevent other clients from accessing the resource.

2. Resource Management: HTTP can only access a single resource directly. WEBDAV provides a method of organizing data more efficiently. WebDAV introduces a collection of resources (similar to the file system folder) concept. Resource management through WebDAV includes the following functions: Creating, Moving, Copying, and Delete a collection, as well as resources or files in the collection. 3. Document Properties: Different types of data have unique properties, which helps to describe data. For example, in an email, these properties may be the time of the sender's name and the time of receiving the mail. In collaboration documents, these properties may be the name of the original author of the document and the name of the last editor. Because people's document types are different, the list of possible attribute types can also become unlimited. XML is an expandable communication tool required for WebDAV.

For more information on WebDAV, please refer to the last reference of this article.

Compared to self-resolving Hotmail's browser page, the interface based on specific protocol WebDAV makes the Hotmail mail client program more stable and reliable. In the case of the basic interface of the JavaMail API and the interface of Hotmail, the topic of this article becomes very specific, which is if the WebDAV-based JavaMail Provider (JavaMail provider) is written and use this provider to implement Hotmail mail transceiver function. However, this topic is too big and has exceeded the scope of the article that can be described, so we will introduce an open source project JDAVMail, and details how to implement Hotmail mail through this project.

JDAVMail is an open source JavaMail provider project, which can access WebDAV protocol-based mail services, such as a Hotmail mailbox. You can download to the latest version of the program at http://sourceforge.net/projects/jdavmail this URL. JDAVMail uses the Commons HttpClient on Jakarta as the HTTP client component to communicate with the server, and uses JDM for XML data, and since the HTTPClient project uses the output of Commons Logging for the Diary, it is necessary, this is necessary. All three components can be found in the lib directory in the JDAVMail's compressed package, and the compressed package also includes compiled JAR files and all source code.

In order to make everyone have a probably aware of JDAVmail, let's give two code for the sending and reception of Hotmail mail:

Email charge

/ *

* File name: Hotmaildemo.java

* Create time: 2004-9-14

* Created by: liudong

* /

Package com.clickcom.mail;

Import java.util.date;

Import java.util.properties;

Import javax.mail.folder;

Import javax.mail.MAESSAGE;

Import javax.mail.session;

Import javax.mail.store;

Import javax.mail.transport;

Import javax.mail.internet.internetAddress;

Import javax.mail.internet.mimemessage;

/ **

* Hotmail mail transceiver

* @Author liudong

* /

Public class hotmaildemo {

Public static void main (String [] args) {

Receive ();

}

/ **

* Email Receive

* /

protected static void receive () {try {

Properties Prop = New Properties ();

Session SES = session.getinstance (Prop);

// Use JDAVMail Provider

Store Store = SES.GETSTORE ("DAVMAIL");

/ / No specified server address

Store.Connect (NULL, "Your Account", "Password");

IF (store.isconnected ()) {

Folder Inbox = Store.getFolder ("Inbox");

IF (Inbox.exists ()) {

Inbox.open (Folder.Read_only);

Int ncount = inbox.getMessageCount ();

System.out.println ("Inbox Contains" NCOUNT "Messages");

// Show each email in the inbox

For (INT i = 1; i <= ncount; i ) {

MimeMessage Msg = (MIMEMESSAGE) Inbox.getMessage (i);

System.out.println ("Subject:" msg.getsubject ());

System.out.println ("from:" msg.getFrom () [0] .tostring ());

System.out.println ("Content Type:" msg.getContentType ());

System.out.println (msg.getContent ());

}

}

}

} catch (exception ex) {

EX.PrintStackTrace ();

}

}

}

From the above code we found that in addition to the Store Store = SES.GetStore ("DAVMAIL"); this statement does not need to specify the server address, there is no two normal JavaMail applications, nor introduced any JDAVMail The class is related. This is relatively easy to specify the server address because the address of Hotmail provides the HTTPMAIL service is fixed. In fact, only getStore is a bit different from the regular mail client program that it uses the DAVMAIL string as a parameter, and we specify the parameter value POP3 when using JavaMail to connect to the POP3 server. What's going on? Because JavaMail is to load the Provider class through the protocol name. Open JDAVMail.0.9.006.jar This JAR file will find that there is a file JavaMail.Providers in the meta-INF directory, which is as follows:

Protocol = davmail; type = store; class = com.posift.jdavmail.jdavmailstore; vendor = posTIVE SOFTWARE;

Protocol = DAVMAIL_XMIT; type = transport; class = com.posift.jdavmail.jdavmailTransport; vendor = posTIIVE SOFTWARE;

Therefore, when we specify the protocol named DAVMail, Javamail will automatically load the com.posiSoft.jdavmail.jdavMailStore class, so the Store instance obtained by incoming Davmail strings is actually class com.posiSoft.jdavmail.jdavMailstore An example so I think everyone should understand the working principle of the previous program. Let's take a look at JDAVmail's code snippet:

/ **

* Email

* /

protected static void send () {

Try {

Properties Prop = New Properties ();

// mail sender address

Prop.SetProperty ("mail.davmail.from", "abc@hotmail.com");

Session SES = session.getinstance (Prop);

// Get a mail sending example of JDAVMail

Transport Transport = SES.GETTRANSPORT ("DAVMAIL_XMIT");

/ / Connect to the Hotmail server, replace it with your own username and password

Transport.connect (NULL, "User Name", "Password");

// Prepare the mail to send

MimeMessage TXMSG = New MimeMessage (SES);

TXMSG.SETSUBJECT ("this is the subject");

// mail sender address

InternetAddress AddrFrom = New InternetAddress ("abc@hotmail.com");

TXMsg.SetFrom (AddRFROM);

// mail recipient address

InternetAddress AddRTO = New InternetDress ("CDEf@hotmail.com", "CDEF");

Txmsg.addrecipient (Message.RecipientType.to, AddRTo);

//content of email

TXMSG.Settext ("Hello World!");

TXMSG.SetSentDate (New Date ());

//send email

Transport.sendMessage (TXMSG, TXMsg.GetallRecipients ());

} catch (exception ex) {

EX.PrintStackTrace ();

}

}

Similarly, this code uses the DavMail_Xmit protocol to get a mail sending an instance of JDAVMail, which is not difficult to know the JDAVMail using the JavaMail.jdavMail.jdavMailTransport, which is not difficult to know by the javamail.providers file mentioned earlier.

It was found in actual testing whether it was JDAVMail or other mail tools. The transmission speed of the message using Hotmail was slow, and almost a few minutes before I received the corresponding information, so if the problem was found in the test, please be patient.

I want to introduce this article how to use JDAVMail to send and receive hotmail mail, and study JDAVmail source code, not only know more about Javamail architectures, but also have more specific references to prepare JavaMail Provider. In addition, I hope that my friends who are distinguished from how to access Hotmail mail in the Java program. At the same time, I also welcome my website http://www.javayou.com to communicate with me in use.

Reference

JDAVMail website http://jdavmail.sourceforge.net/ About WebDav more detailed information http://www.webdav.org/ No Chinese fettered Using JavaMail to charge mail http://www.javayou.com/showlog.jspe • log_id = 372 Using JavaMail's mail sending component http://www.javayou.com/showlog.jspe?log_id=136 About the author Liu Dong, has been using J2EE to engage in mobile business development. You can now contact me through the Java free-owner website, the URL is: http://www.javayou.com; the addition of my email is winter.lau@163.com

转载请注明原文地址:https://www.9cbs.com/read-105819.html

New Post(0)