附上PDF / Doc文件和我的邮件

时间:2012-04-25 04:57:56

标签: iphone ios xcode email email-attachments

我已在线生成pdf文件。在看pdf时,我想通过邮件发送该pdf,并自动附上该pdf。我使用了很多代码,但是对于单个pdf.can一切正常。任何人都可以帮助我。

3 个答案:

答案 0 :(得分:9)

试试这个,

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");
}

答案 1 :(得分:1)

谢谢@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
        })
    }

答案 2 :(得分:0)

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"];