Email sending code for iOS development

Source: Internet
Author: User

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];
}

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.