Android-mail API usage

Source: Internet
Author: User
Tags email account mail account

This is the most complex and applicable example officially provided, demonstrating the implementation of the mail function. This is the last entry-level learning note for ophone. All examples are from the official sample file. After the end of this article, we will introduce a slightly complex topic explanation. Thank you for your attention.

I. Overall Structure

By observing the overall structure of the project, we can see that the project has three java files. Because the program has an interface for configuring account information besides the main program, configaccountlayout is used. java file.

II. Introduction to the main interface
Public class mailapidemoactivity extends activity implements pop3processlistener
This program applies the pop3processlistener interface, and friends with Java basics should be familiar with this. This interface requires the voidreceivedmailmessage (baseaccountinfo accountinfo, cmmail m) function to be implemented as a response to receive the mail message.
The function content is: If (mail! = NULL) printlog ("received new mail: \ n" + mail. tostring ());

Some variable constants are defined as follows:
// The basic account information is defined here, which is the mail function class provided by ophone.
Public baseaccountinfo accountinfo;
// This listener can follow the mail task status
Public maileventlistenerclass maileventlistener;
Public View mainview;

Public static final string tag = "mailexample ";
Public static final string tag_cmmail = "cmmail ";
Public static final string tag_pop3processlistener = "pop3processlistener :";

Next is the oncreate code:

Mainview and textview are defined on the main interface. In addition, the listener and account information of mail messages are also defined.
Next, we define three buttons: Configure mail, receive mail, and send mail.

1. Configure email
Button configbutton = (button) findviewbyid (R. Id. button_config_account );
Configbutton. setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
Configaccountlayout view = (configaccountlayout) configaccountlayout. Inflate (
Mailapidemoactivity. This, R. layout. config_account, null );
View. setaccountinfo (accountinfo );
Setcontentview (View );
}
});
From the code above, we can see that the main flow of the button code is: button definition, listener creation, click event configuration, and Event Response content setting (such as switching to configaccountlayout ). When creating configaccountlayout, we use the inflate function, which can generate a new view from an existing resource extension. The function is defined as follows:
View Android. View. View. Inflate (context, int resource, viewgroup root)
The setaccountinfo function is defined in configaccountlayout. Java and is used to set account information. We will analyze it later. Finally, switch the current view to the configured mail view.

2. receive emails
Button popbutton = (button) findviewbyid (R. Id. button_pop );
Popbutton. setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
If (accountinfo = NULL ){
Printlog ("Please config account first! ");
Return;
}
Printlog ("START fetch new mail ...");
// Obtain the server mail message
Cmmailtaskcontroller. getinstance (). startfetchnewmessages (mailapidemoactivity. This, accountinfo, null, maileventlistener, mailapidemoactivity. This );
}
});
The mail receiving function is easy to understand. First, check the account configuration and then execute the function through the mail receiving function.

3. Send an email
Button smtpbutton = (button) findviewbyid (R. Id. button_smtp );
Smtpbutton. setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
If (accountinfo = NULL ){
Printlog ("Please config account first! ");
Return;
}
Try {
// Create a mail instance
Cmmail mail = new cmmail ();
// Add the sending Address
Edittext inputto = (edittext) mailapidemoactivity. This. findviewbyid (R. Id. input_to );
String toaddress = inputto. gettext (). tostring ();
If (toaddress = NULL | toaddress. Length () = 0 ){
Printlog ("Please input address first ");
Return;
}
Cmmailaddress toadd = new cmmailaddress (null, toaddress );
Mail. addtoaddress (toadd );

// Set the Email Subject
Mail. setsubject ("oms api test mail ");

// Set the email content
Mail. setbodytxt ("Hello! \ Nthis is a mail from oms api tester! ");

// Create a sending list
Cmmail [] sendmails = new cmmail [] {mail };

// Start sending messages
Cmmailtaskcontroller. getinstance (). startsendmessages (mailapidemoactivity. This, accountinfo, sendmails, maileventlistener );

Printlog ("posted mail to:" + toadd. getaddress () + "; Subject:" + mail. getsubject ());

} Catch (exception e ){
Printlog (tag_pop3processlistener + E. tostring ());
Log. D (TAG, tag_pop3processlistener + E. tostring ());
}
}
});
}

The main interface also contains a class to analyze the mail status response:
Public class maileventlistenerclass implements maileventlistener
It needs to implement the maileventlistener interface. The content of this interface function is as follows:


Events are divided into three types: Login, receive mail, receive with attachment mail, the list of specific events can be viewed: http://www.ophonesdn.com/documentation/ophone/reference/ophoneapi/mail/oms/mail/MailBaseEvent.html

At the end of the main program, there are two functions to implement the log printing function.

Putstring is used to insert text "text" into string text. As the keyword extracted by handler in the message queue.


Handler can process and send messages or running objects related to message queues. Handlemessage is a required interface for receiving and processing messages. In the handlerlog, the MSG is displayed in the view. In printlog, The sendmessage function adds a message to the end of the message queue.

Iii. Email account Configuration

The mail account configuration function is implemented in another configaccountlayout. Java file. It is extends in relativelayout, that is, the component location on it can be described with the relative location of other components or fathers. Context is the global environment of the application, that is, configaccountlayout. Java uses the context of mailapidemoactivity.

When a view is attached to a form, the onattachedtowindow method is called. This method is complete: 1. Define the control, 2. initialize the account information, and 3. Define the OK cancel button. These codes are easy to understand. You can see that the OK button contains the function getaccount.

This function is an ophone built-in function that writes the obtained mail configuration parameters. The specific functions and parameters of each function can be referred to: http://www.ophonesdn.com/documentation/ophone/reference/ophoneapi/mail/oms/mail/BaseAccountInfo.html

The following figure shows the running result of the configuration interface:

Iv. Supplementary information

Finally, post some mail toolkit introduction from superzhang translation, http://www.superzhang.com.cn/viewthread.php? Tid = 67 & extra = Page % 3d1, API original address http://www.ophonesdn.com/documentation/ophone/reference/ophoneapi/mail/oms/mail/package-summary.html

Package OMS. Mail
This package provides all the APIs for receiving and sending emails.

Interface
Maileventlistener
Email event listening interface.
Pop3processlistener
Email receiving interface.

Class
Baseaccountinfo
The base class of account information, including the setting of all parameters.
Cmmail
It includes some methods for setting and obtaining the content of each part of the email.
Cmmailaddress
Email Address class. You can set and obtain email addresses.
Cmmailbodypart
Email content
Cmmailtaskcontroller
Provides an instance to manage and run mail tasks, such as receiving and deleting emails.
Mailbaseevent
Email event class.
Mailbaseevent. taskstatus
Status information of the send and receive mail task.

Exception
Mailexception
Normal email exception

Related Article

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.