你能在Android和iOS上以编程方式发送带有正文和附件的电子邮件吗?



我知道你可以在Android上以编程方式发送电子邮件,并像这样预填所有内容:

    final Intent emailIntent = new Intent(android.content.Intent. ACTION_SENDTO);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, "emailaddress@emailaddress.com");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png));
    emailIntent.setType("image/png");
    startActivity(Intent.createChooser(emailIntent, getString(R.string.send_intent_name)));

我想确认您是否可以在iOS上执行相同的操作。如果两个平台都将您带到其默认的电子邮件客户端,或者是否可以在后台执行任何操作。

在iOS上发送邮件,代码如下:

- (IBAction)sendEmail:(id)sender {
    //email subject
    NSString * subject = @"send mail for iOS";
    //email body
    NSString * body = @"Hello";
    //recipient(s)
    NSArray * recipients = [NSArray arrayWithObjects:@"account@domain.com", nil];
    //create the MFMailComposeViewController
    MFMailComposeViewController * composer = [[MFMailComposeViewController alloc] init];
    composer.mailComposeDelegate = self;
    [composer setSubject:subject];
    [composer setMessageBody:body isHTML:NO];
    //[composer setMessageBody:body isHTML:YES]; //if you want to send an HTML message
    [composer setToRecipients:recipients];
    //get the filepath from resources
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png"];
    //read the file using NSData
    NSData * fileData = [NSData dataWithContentsOfFile:filePath];
    // Set the MIME type
    /*you can use :
     - @"application/msword" for MS Word
     - @"application/vnd.ms-powerpoint" for PowerPoint
     - @"text/html" for HTML file
     - @"application/pdf" for PDF document
     - @"image/jpeg" for JPEG/JPG images
     */
    NSString *mimeType = @"image/png";
    //add attachement
    [composer addAttachmentData:fileData mimeType:mimeType fileName:filePath];
    //present it on the screen
    [self presentViewController:composer animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled"); break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved"); break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent"); break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]); break;
        default:
            break;
    }
    // close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

我想确认您是否可以在iOS上执行相同的操作

是的。

如果两个平台都将您带到其默认电子邮件客户端

不是默认的电子邮件客户端,但Android为用户提供了一个选项,可以选择能够处理操作的客户端。在iOS中,您使用MessageUI框架,该框架提供了应用程序内邮件编辑器MFMailComposeViewController

如果有可能在后台执行任何操作

如果您的意思是在没有用户干预的情况下发送电子邮件,当然是可能的。但是,您将无法从为用户配置的电子邮件帐户发送邮件。

最新更新