iOS - 将本地HTML页面附加到电子邮件

时间:2012-11-10 12:07:12

标签: html ios email

是否允许将本地存储的HTML页面附加到从我的iOS应用程序发送的电子邮件中?

什么是正确的mimeType?

修改

这是我的代码无法正常工作,请在哪里找出阻止本地html文件附件的问题。

-(IBAction)share:(id)sender{


NSString *btn_title = [sender titleForState:(UIControlStateNormal)];

 if ([btn_title isEqualToString: @"fb"] ) {


 }else if ([btn_title isEqualToString: @"tw"]){


 }else if ([btn_title isEqualToString: @"email"]){

     if ([MFMailComposeViewController canSendMail])
     {
         MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
         mailer.mailComposeDelegate = self;
         [mailer setSubject:@""];
         NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
         [mailer setToRecipients:toRecipients];

         NSString *emailBody = @"";

         NSError * error = nil;
         NSData *htmlData = [NSData dataWithContentsOfFile:@"/hamla.html" options: NSMappedRead error: &error];

         [mailer addAttachmentData:htmlData mimeType:@"text/html" fileName:@"hamla"];

         [mailer setMessageBody:emailBody isHTML:NO];
         [self presentModalViewController:mailer animated:YES];
     }
     else
     {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                         message:@"Your device doesn't support the composer sheet"
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles: nil];
         [alert show];
     }
 }

}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }
    // Remove the mail view
    [self dismissModalViewControllerAnimated:YES];
}

3 个答案:

答案 0 :(得分:0)

我不明白为什么会出现问题。

mimetype是' text / html'。看看这里:

http://reference.sitepoint.com/html/mime-types-full

答案 1 :(得分:0)

@Shymaa Othman使用的MIME类型是正确的。只需要知道你是否正确定位了你的html并且得到的NSData不是空的? 如果没有,请在附加到邮件之前尝试此代码。

NSData NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"hamla" ofType:@"html"];

NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];

答案 2 :(得分:0)

当我使用isHTML = YES的setMessageBody加上文本附件时,它会起作用。

NSData *stringData = [textBody dataUsingEncoding:NSUTF8StringEncoding];
[mailer addAttachmentData:stringData
                 mimeType:@"text/plain"
                 fileName:@"text_file"];