iOS我可以在邮件应用程序发送的电子邮件中注入HTML代码吗?

时间:2011-08-17 15:44:40

标签: iphone ios email

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

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

4 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:1)

它应该看起来像这样:

    // 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 马库斯

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

您可以获取其电子邮件的正文并进行更改(使用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的电子邮件。