网址链接未通过电子邮件发送

时间:2013-01-03 13:42:59

标签: iphone objective-c ios mfmailcomposeviewcontroller

在我的iphone应用程序中,在MFMailComposer中,我添加了一个附件。该附件作为URL链接。在该链接中有一个pdf文件。当我点击发送时,网址链接不会发送到目标邮箱地址。

我正在使用此代码

  -(void)sendMail
 {
    MFMailComposeViewController *mailView= [[MFMailComposeViewController alloc] init];
    mailView.mailComposeDelegate=self;
    [mailView setSubject:titleString];
    NSArray  *array = [[NSArray alloc]initWithObjects:emailString, nil];
    [mailView setToRecipients:array];
    NSData *textData = [NSData dataWithContentsOfFile:self.fileString];
    [mailView addAttachmentData:textData mimeType:@"text/plain" fileName:self.fileString];

    [mailView setMessageBody:self.templatetextstring isHTML:YES];
    [self presentModalViewController:mailView animated:YES];
 }

2 个答案:

答案 0 :(得分:2)

使用此代码

NSString *link = [NSString stringWithFormat:@"http://www.google.com"];
[controller setMessageBody:[NSString stringWithFormat:@"<p><font size=\"2\" face=\"Helvetica\"><a href=%@></br>%@</br></a></br></font></p>",link,@"Google"] isHTML:YES];

希望它可以帮助你..

答案 1 :(得分:1)

我不确定我是否理解您的问题,可能是您要附上pdf文档,还是只想添加pdf文档的链接,以便我回答这两个问题。这是我用来将pdf数据附加到电子邮件中的代码

- (void)emailFile
{
    if(![MFMailComposeViewController canSendMail]) {
        UIAlertView *cantSend = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Device not configured to send email" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
       [cantSend show];
    } else {
        MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
        mailView.mailComposeDelegate = self;

        [mailView setSubject:@"PDF Attached to email"];

        // Adding an actual PDF document to the email.
        [mailView addAttachmentData:(__bridge NSData *)myPDFData mimeType:@"pdf" fileName:@"AttachedPDFDocument"];

        [mailView setMessageBody:[NSString stringWithFormat:@"Sending %@. This email maybe sent as junk mail",fileName] isHTML:NO];
        [self presentModalViewController:mailView animated:YES];
     }
}

注意我正在添加pdf数据而不是实际的pdf,然后我将扩展名(MimeType)设置为pdf,然后设置文件的名称,这样就可以将附件添加到正在构建的电子邮件中。< / p>

要添加指向电子邮件的链接,就像

一样简单
    - (void)emailFile
{
    if(![MFMailComposeViewController canSendMail]) {
        UIAlertView *cantSend = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Device not configured to send email" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
       [cantSend show];
    } else {
        MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
        mailView.mailComposeDelegate = self;

        [mailView setSubject:@"PDF Link added in HTML"];

        // Adding a HTML Link to an email. Remembering to set the Message to allow HTML.
        NSString *link = [NSString stringWithFormat:@"http://www.google.com"];
        [mailView setMessageBody:[NSString stringWithFormat:@"<p><font size=\"2\" face=\"Helvetica\"><a href=%@></br>%@</br></a></br></font></p>",link,@"Google"] isHTML:YES];

        [self presentModalViewController:mailView animated:YES];
     }
}

请注意,您没有附加任何数据,因为您将setMessage设置为使用HTML,而第一个示例不使用HTML。现在,您可以在邮件正文中设置包含html元素的NSString

编辑

myPDFData是我从网络服务下载的PDF的dataContent的CFDataRef,然后用户可以通过电子邮件将其转发给自己。如果您使用的是ARC,那么在设置attachmentData时,您需要在(__bridge NSData *)myPDFData中添加网桥。

希望这有帮助。