---restore content starts---
Method One:
1. Need to introduce library messageui.framework
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
[Email protected] Viewcontroller:uixxxxxviewcontroller <.., mfmailcomposeviewcontrollerdelegate>
@end
3. Send the execution code. Verify the relevant support beforehand.
Class Mailclass = (nsclassfromstring (@ "Mfmailcomposeviewcontroller")); if (!mailclass) {Uialertview *alert = [[[Uialertview alloc] initwithtitle:@ "Send Mail" message:@ "The current system version does not support in-app send mail functionality, you can use the Mailto method instead of" Delegate:self cancelbuttontitle:@ "I know." Otherbuttontitles:nil] autorelease]; [Alert show]; Return } if (![ Mailclass Cansendmail]) {Uialertview *alert = [[[Uialertview alloc] initwithtitle:@ "Send Mail" message:@ "User does not have a mail account set up" delegate:self cancelbuttontitle:@ "I know," oth. Erbuttontitles:nil] autorelease]; [Alert show]; Return } Mfmailcomposeviewcontroller *MC = [[Mfmailcomposeviewcontroller alloc] init]; Mc.mailcomposedelegate = self; [MC setsubject:@ "Hello, world!"]; [MC Settorecipients:[nsarray arraywithobject:@ "[email protected]"]];//[MC Setccrecipients:[nsarray arraywithobject:@ "[email protected]"]];//[MC setbccrecipients:[nsarray arraywithobject:@ "[email protected] []]; [MC setmessagebody:@ "Hello,slick!!! \n\ncome here, I need you! "Ishtml:no";
Add a picture UIImage *addpic = [UIImage imagenamed: @ "[email protected]"]; NSData *imagedata = uiimagepngrepresentation (addpic); PNG [MC addattachmentdata:imagedata MimeType: @ "" FileName: @ "Icon.png"]; Add a PDF attachment nsstring *file = [self fullbundlepathfromrelativepath:@ "high quality C + + Programming Guide. pdf"]; NSData *pdf = [NSData datawithcontentsoffile:file]; [MC addattachmentdata:pdf MimeType: @ "" FileName: @ "High quality C + + Programming Guide. pdf"]; [Self PRESENTVIEWCONTROLLER:MC animated:yes completion:nil]; [MC release];
callback function:
-(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 Dismissviewcontrolleranimated:yes completion:nil];}
Method Two:
Url method
#pragma mark-Send mail using System mail client-(void) Launchmailapp {nsmutablestring *mailurl = [[Nsmutablestring alloc]init]a Utorelease]; Add recipient Nsarray *torecipients = [Nsarray arraywithobject: @ "[email protected]"]; [Mailurl appendformat:@ "mailto:%@", [ToRecipients componentsjoinedbystring:@ ","]]; Add cc Nsarray *ccrecipients = [Nsarray arraywithobjects:@ "[email protected]", @ "[email protected]", nil]; [Mailurl appendformat:@ "? cc=%@", [CCRecipients componentsjoinedbystring:@ ","]]; Add encryption to send Nsarray *bccrecipients = [Nsarray arraywithobjects:@ "[email protected]", nil]; [Mailurl appendformat:@ "&bcc=%@", [bccrecipients componentsjoinedbystring:@ ","]]; Add theme [Mailurl appendstring:@ "&subject=my Email"]; Add message content [Mailurl appendstring:@ "&body=<b>email</b> body!"]; nsstring* email = [Mailurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];[[UIApplication Sharedapplication] OpenURL: [Nsurl Urlwithstring:email]]; }
[[uiapplicationsharedapplication] openURL: [nsurlurlwithstring: @ "Mailto:[email Protected][email protected]&subject=greetings%20from%20cupertino!&body=wish%20you%20were%20here! " ]];
Third-party controls such as Skpsmtpmessage can also be used.
---restore content ends---
Method One:
1. Need to introduce library messageui.framework
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
[Email protected] Viewcontroller:uixxxxxviewcontroller <.., mfmailcomposeviewcontrollerdelegate>
@end
3. Send the execution code. Verify the relevant support beforehand.
Class Mailclass = (nsclassfromstring (@ "Mfmailcomposeviewcontroller")); if (!mailclass) {Uialertview *alert = [[[Uialertview alloc] initwithtitle:@ "Send Mail" message:@ "The current system version does not support in-app send mail functionality, you can use the Mailto method instead of" Delegate:self cancelbuttontitle:@ "I know." Otherbuttontitles:nil] autorelease]; [Alert show]; Return } if (![ Mailclass Cansendmail]) {Uialertview *alert = [[[Uialertview alloc] initwithtitle:@ "Send Mail" message:@ "User does not have a mail account set up" delegate:self cancelbuttontitle:@ "I know," oth. Erbuttontitles:nil] autorelease]; [Alert show]; Return } Mfmailcomposeviewcontroller *MC = [[Mfmailcomposeviewcontroller alloc] init]; Mc.mailcomposedelegate = self; [MC setsubject:@ "Hello, world!"]; [MC Settorecipients:[nsarray arraywithobject:@ "[email protected]"]];//[MC Setccrecipients:[nsarray arraywithobject:@ "[email protected]"]];//[MC setbccrecipients:[nsarray arraywithobject:@ "[email protected] []]; [MC setmessagebody:@ "Hello,slick!!! \n\ncome here, I need you! "Ishtml:no";
Add a picture UIImage *addpic = [UIImage imagenamed: @ "[email protected]"]; NSData *imagedata = uiimagepngrepresentation (addpic); PNG [MC addattachmentdata:imagedata MimeType: @ "" FileName: @ "Icon.png"]; Add a PDF attachment nsstring *file = [self fullbundlepathfromrelativepath:@ "high quality C + + Programming Guide. pdf"]; NSData *pdf = [NSData datawithcontentsoffile:file]; [MC addattachmentdata:pdf MimeType: @ "" FileName: @ "High quality C + + Programming Guide. pdf"]; [Self PRESENTVIEWCONTROLLER:MC animated:yes completion:nil]; [MC release];
callback function:
-(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 Dismissviewcontrolleranimated:yes completion:nil];}
Method Two:
Url method
#pragma mark-Send mail using System mail client-(void) Launchmailapp {nsmutablestring *mailurl = [[Nsmutablestring alloc]init]a Utorelease]; Add recipient Nsarray *torecipients = [Nsarray arraywithobject: @ "[email protected]"]; [Mailurl appendformat:@ "mailto:%@", [ToRecipients componentsjoinedbystring:@ ","]]; Add cc Nsarray *ccrecipients = [Nsarray arraywithobjects:@ "[email protected]", @ "[email protected]", nil]; [Mailurl appendformat:@ "? cc=%@", [CCRecipients componentsjoinedbystring:@ ","]]; Add encryption to send Nsarray *bccrecipients = [Nsarray arraywithobjects:@ "[email protected]", nil]; [Mailurl appendformat:@ "&bcc=%@", [bccrecipients componentsjoinedbystring:@ ","]]; Add theme [Mailurl appendstring:@ "&subject=my Email"]; Add message content [Mailurl appendstring:@ "&body=<b>email</b> body!"]; nsstring* email = [Mailurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];[[UIApplication Sharedapplication] OpenURL: [Nsurl Urlwithstring:email]]; }
[[uiapplicationsharedapplication] openURL: [nsurlurlwithstring: @ "Mailto:[email Protected][email protected]&subject=greetings%20from%20cupertino!&body=wish%20you%20were%20here! " ]];
Third-party controls such as Skpsmtpmessage can also be used.
iOS Send mail