iOS Send mail

Source: Internet
Author: User
Tags mail account

---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

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.