The mail sending function is provided by messageui framework, which is the simplest box in the iPhone SDK. It consists of a class, A View Controller, and a protocol.
1. Create a View Controller:
Mfmailcomposeviewcontroller * MC = [[mfmailcomposeviewcontroller alloc] init]; Mc. mailcomposedelegate = self;
Ii. Set Email Subject:
[MC setsubject:@"Hello, world!"];
3. Set recipients. There are three types of recipients:
1. Set the primary recipient
[MC settorecipients: [nsarray arraywithobjects: @ "zhuqi0@126.com", "@ dave@iphonedevbook.com", nil];
2. Set CC
[MC setccrecipients: [nsarray arraywithobject:@"Zhuqil@163.com"];
3. Set BCC
[MC setbccrecipients: [nsarray arraywithobject:@"Secret@gmail.com"];
4. Set the email subject in two formats.
One is plain text
[MC setmessagebody:@"Watson !!! \ N \ ncome here, I need you!"Ishtml: No];
One is in HTML format.
[MC setmessagebody: @ "<HTML> <B> hello, Joe! </B> <br/> what do you know? </Html> "ishtml: Yes];
5. Add attachments
Adding an attachment requires three parameters: an attachment of the nsdata type, a MIME type, and an attachment name.
Nsstring * Path = [[Nsbundle mainbundle] pathforresource: @" Blood_orange "
Oftype: @" PNG " ];
Nsdata * Data = [Nsdata datawithcontentsoffile: path];
[MC addattachmentdata: Data mimetype: @" Image/PNG " Filename: @" Blood_orange " ];
Vi. View presentation
[Self presentmodalviewcontroller: Mc animated: Yes];
[MC release];
VII. View Controller delegation Method
The delegate method of the mail View Controller is included in mfmailcomposeviewcontrollerdelegate. Whether or not the user sends or cancels the message, whether or not the system can send the mail,
Method mailcomposecontroller: didfinishwithresult: Error: gets called will be called.
- ( Void ) Mailcomposecontroller :( mfmailcomposeviewcontroller * ) Controller
Didfinishwithresult :( mfmailcomposeresult) Result
Error :( nserror * ) Error {
Switch (Result)
{
Case Mfmailcomposeresultcancelled:
Nslog ( @" Mail Send canceled... " );
Break ;
Case Mfmailcomposeresultsaved:
Nslog ( @" Mail saved... " );
Break ;
Case Mfmailcomposeresultsent:
Nslog ( @" Mail sent... " );
Break ;
Case Mfmailcomposeresultfailed:
Nslog ( @" Mail Send errored: % @... " , [Error localizeddescription]);
Break ;
Default :
Break ;
}
[Self dismissmodalviewcontrolleranimated: Yes];
}