你能否在Android上以编程方式发送包含正文和附件的电子邮件; iOS版?

时间:2015-04-09 13:50:02

标签: android ios email

我知道您可以在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上做同样的事情。如果两个平台都将您带到他们的默认电子邮件客户端,或者它可以在后台执行任何操作。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

  

我想确认你是否可以在iOS上做同样的事情

  

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

不是默认电子邮件客户端,但Android为用户提供了选择能够处理操作的客户端的选项。在iOS中,您使用MessageUI Framework,它提供MFMailComposeViewController这是一个应用内邮件编写器。

  

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

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