From http://blog.csdn.net/koupoo/article/details/6455289
AddMessageui.Framework # Import <messageui/messageui. h> Mfmailcomposeviewcontrollerdelegate
CodeAs follows:
# PragmaMark-
# PragmaMarkMfmailcomposeviewcontroller
- (Void)Alertwithtitle Nsstring *)_ Title _ msg Nsstring *)MSG{
Uialertview *Alert= [[UialertviewAlloc]Initwithtitle:_ Title _
Message:MSG
Delegate:Nil
Cancelbuttontitle:@"OK"
Otherbuttontitles:Nil];
[Alert show];
[Alert release];
}
-(Void)Displaycomposersheet{
Mfmailcomposeviewcontroller *Mailpicker= [[MfmailcomposeviewcontrollerAlloc]Init];
Mailpicker.Mailcomposedelegate= Self;
[Mailpicker setsubject:@"Email Subject"];
// Add a sender
Nsarray *Torecipients= [NsarrayArraywithobject:@" First@example.com "];
// Nsarray * ccrecipients = [nsarray arraywithobjects :@" Second@example.com ",@" Third@example.com ", Nil];
// Nsarray * bccrecipients = [nsarray arraywithobject :@" Fourth@example.com ", Nil];
[Mailpicker settorecipients:Torecipients];
// [Picker setccrecipients: ccrecipients];
// [Picker setbccrecipients: bccrecipients];
// Add an image
Uiimage *Addpic= [UiimageImagenamed:@"Icon.png"];
Nsdata *Imagedata= Uiimagepngrepresentation(Addpic);// PNG
// Nsdata * imagedata = uiimagejpegrepresentation (addpic, 1); // JPEG
[Mailpicker addattachmentdata:Imagedata mimetype:@""Filename:@"Icon.png"];
Nsstring *Emailbody= @"Email body";
[Mailpicker setmessagebody:Emailbody ishtml:Yes];
[SelfPresentmodalviewcontroller:Mailpicker animated:Yes];
[Mailpicker release];
}
-(Void)Launchmailappondevice{
Nsstring *Recipients= @"Mailto: first@example.com & subject = my email! ";
// @ "Mailto: first@example.com? Cc = second@example.com, Third@example.com & Amp; subject = my email! ";
Nsstring *Body= @"& Body = Email body! ";
Nsstring *Email= [NsstringStringwithformat:@"% @",Recipients,Body];
Email= [Email stringbyaddingpercentescapesusingencoding:Nsutf8stringencoding];
[[UiapplicationSharedapplication]Openurl:[NsurlUrlwithstring:Email];
}
-(Void)Sendemail{
ClassMailclass= (Nsclassfromstring(@"Mfmailcomposeviewcontroller"));
If (Mailclass! = Nil) {
If ([Mailclass cansendmail]) {
[SelfDisplaycomposersheet];
} Else {
[SelfLaunchmailappondevice];
}
} Else {
[SelfLaunchmailappondevice];
}
}
- (Void)Mailcomposecontroller Mfmailcomposeviewcontroller *)Controller
Didfinishwithresult Mfmailcomposeresult)Result Error Nserror *)Error{
Nsstring *MSG;
Switch (Result) {
Case Mfmailcomposeresultcancelled:
MSG= @"Cancel email sending";
Break;
Case Mfmailcomposeresultsaved:
MSG= @"The email is successfully saved";
[SelfAlertwithtitle:NilMSG:MSG];
Break;
Case Mfmailcomposeresultsent:
MSG= @"Email sent successfully";
[SelfAlertwithtitle:NilMSG:MSG];
Break;
Case Mfmailcomposeresultfailed:
MSG= @"Email sending failed";
[SelfAlertwithtitle:NilMSG:MSG];
Break;
Default:
Break;
}
Nslog(@"Sending result: % @",MSG);
[SelfDismissmodalviewcontrolleranimated:Yes];
}