xcode 在我的邮件中附加 PDF/Doc 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10309555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:05:13  来源:igfitidea点击:

Attach a PDF/Doc file With my mail

iphoneiosxcodeemailemail-attachments

提问by New Xcoder

I have Generated pdf Files from online. While seeing the pdf, i want to send that pdf via mail with that pdf attached automatically. I used lot of codes but everything works fine for single pdf.can any one help me .

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

回答by Gypsa

Try this,

尝试这个,

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

回答by Sankalp S Raul

Thanks @Gypsa
Here is the swift code

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

回答by Puneet_foxinfosoft

mime type is change for pdf so use this mime type it worksfine for me

pdf 的 mime 类型已更改,因此使用此 mime 类型对我来说效果很好

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

Then at some point in the future you'll need to pass that pdfData to the MFMailComposeViewController.

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

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