将 PDF/文档文件与我的邮件一起附加



我从网上生成了pdf文件。在查看 pdf 时,我想通过邮件发送该 pdf,并自动附加该 pdf。我使用了很多代码,但对于单个pdf来说一切正常,任何人都可以帮助我吗?

试试这个,

if([MFMailComposeViewController canSendMail]){      
    MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
    mail.mailComposeDelegate=self;
    [mail setSubject:@"Email with attached pdf"];   
    NSString *newFilePath = @"get path where the pdf reside";
    NSData * pdfData = [NSData dataWithContentsOfFile:newFilePath];
[mail addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"yourpdfname.pdf"];
    NSString * body = @"";
    [mail setMessageBody:body isHTML:NO];
    [self presentModalViewController:mail animated:YES];
    [mail release];         
}
else
{
    //NSLog(@"Message cannot be sent");
}

谢谢@Gypsa
这是快速代码

func composeMail(){        
        if(MFMailComposeViewController.canSendMail()){
            var mail:MFMailComposeViewController = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setSubject("Email with attached pdf")
            //file name "attatchment.pdf" in project bundle
            var newFilePath:NSString = NSBundle.mainBundle().pathForResource("attatchment", ofType: "pdf")!
            var pdfData:NSData = NSData(contentsOfFile: newFilePath as String)!
            mail.addAttachmentData(pdfData, mimeType: "application/pdf", fileName: "attatchment.pdf")
            var body:NSString = ""
            mail.setMessageBody(body as String, isHTML: false)
            self.presentViewController(mail, animated: true) { () -> Void in
            }
        }else{
            println("Message cannot be sent")
        }
    }
    // MARK: - MFMailComposeViewControllerDelegate
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)
    {
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
        })
    }

mime 类型是 pdf 的更改,所以使用这个 MIME 类型对我来说很好用

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);

然后在将来的某个时候,您需要将该pdfData传递给MFMailComposeViewController。

MFMailComposeViewController *vc = [[[MFMailComposeViewController alloc] init] autorelease];
[vc setSubject:@"my pdf"];
[vc addAttachmentData:pdfData mimeType:@"image/pdf" fileName:@"SomeFile.pdf"];

相关内容

  • 没有找到相关文章

最新更新