【问题标题】:iOS Can I inject HTML code in the emails send by mail app?iOS 我可以在邮件应用程序发送的电子邮件中注入 HTML 代码吗?
【发布时间】:2011-10-29 01:38:04
【问题描述】:

我们正在经营一家文具业务,并为人们设计电子邮件文具、签名等。在 PC 上使用这些没有问题,因为在大多数邮件客户端中很容易将 HTML 添加到电子邮件内容中。但是,iOS 可以做到这一点吗?

对于移动设备,我们现在如何做到这一点是我们告诉用户添加我们的 SMTP 服务器并通过它发送消息。这个 SMTP 的工作原理是在发送前用 HTML 包装每封电子邮件。我想知道这是否可以直接在 iPhone 中完成,并在发送之前对电子邮件进行某种后处理?

【问题讨论】:

    标签: iphone ios email


    【解决方案1】:

    AFAIK 您无法捕获从内置电子邮件客户端发送的电子邮件。但是,您可以编写自己的应用程序,使用 MFMailComposeViewController 发送带有 HTML 的电子邮件。

    编辑:添加了一些示例代码:

    - (void) sendEventInEmail
    {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
    
        [picker setSubject:@"Email Subject"];
    
        // Fill out the email body text
        NSString *iTunesLink = @"http://itunes.apple.com/gb/app/whats-on-reading/id347859140?mt=8"; // Link to iTune App link
        NSString *content = [eventDictionary objectForKey:@"Description"];
        NSString *imageURL = [eventDictionary objectForKey:@"Image URL"];
        NSString *findOutMoreURL = [eventDictionary objectForKey:@"Link"];
    
        NSString *emailBody = [NSString stringWithFormat:@"<br /> <a href = '%@'> <img src='%@' align=left  style='margin:5px' /> </a> <b>%@</b> <br /><br />%@ <br /><br />Sent using <a href = '%@'>What's On Reading</a> for the iPhone.</p>", findOutMoreURL, imageURL, emailSubject, content, iTunesLink];
    
        [picker setMessageBody:emailBody isHTML:YES];
    
        [self presentModalViewController:picker animated:YES];
    
    [picker release];
        }
    
     - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
        {
            // Notifies users about errors associated with the interface
            switch (result)
            {
                case MFMailComposeResultCancelled:
                    break;
                case MFMailComposeResultSaved:
                    break;
                case MFMailComposeResultSent:
                    break;
                case MFMailComposeResultFailed:
                    break;
                default:
                    break;
            }
            [self dismissModalViewControllerAnimated:YES];
        }
    

    【讨论】:

      【解决方案2】:

      它应该看起来像这样:

          // Compose body
          NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] autorelease];
          [emailBody appendString:@"<p>Hi<br><br>THis is some HTML</p>"];        
          [emailBody appendString:@"</body></html>"];
      
          // Compose dialog
          MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
          emailDialog.mailComposeDelegate = self;
      
          [emailDialog setToRecipients:[NSArray arrayWithObject:@"test@test.com"]];
      
          // Set subject
          [emailDialog setSubject:@"I got a question!"];
      
          // Set body
          [emailDialog setMessageBody:emailBody isHTML:YES];
      
          // Show mail
          [self presentModalViewController:emailDialog animated:YES];
          [emailDialog release];
      

      如上所述,不要忘记委托 8)

      HTH 马库斯

      【讨论】:

        【解决方案3】:

        您可以获取他们电子邮件的正文并对其进行更改(通过使用stringWithFormat: 甚至是 NSMutableString)使其成为 html。使用 MFMailComposeViewController 的-(void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML

        例子:

         NSString *bodyText = [[NSString alloc] initWithFormat:@"<html><p>%@</p></html", bodyField.text];  //Not an HTML expert
        

        然后发送正文为 bodyText 的电子邮件。

        【讨论】:

          【解决方案4】:

          使用 MFMailComposeViewController,您可以在电子邮件正文中包含 HTML。此外,您可以附加图像。请参阅这些方法:

          - (void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML
          - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-11-23
            • 1970-01-01
            • 1970-01-01
            • 2015-06-07
            • 1970-01-01
            • 2012-01-06
            • 1970-01-01
            • 2011-09-10
            相关资源
            最近更新 更多