如何在iPhone应用程序中发送邮件

来源:互联网 发布:网站开发 php java 编辑:程序博客网 时间:2024/04/28 16:14

转自:http://blog.csdn.net/iefreer/article/details/4740517

3.0以前使用mailto URL,但是会退出当前应用程序

 

3.0后Apple提供了MessageUI framework可以在我们的应用程序内实现邮件发送功能,代码示范参见:

https://developer.apple.com/iphone/library/samplecode/MailComposer/index.html

 

可以添加附件和以HTML格式发送邮件。

如果想要在邮件中添加URL如:http://ditu.at/tinyurl

可以如下编码:

 

// Fill out the email body text

NSString *emailBody = [[NSString alloc]init];

NSArray *arSubviews = [shareView subviews];

UITextField *tvMessage = [arSubviews objectAtIndex:4];

emailBody = [NSString stringWithFormat:@"%@,%@ <a href = '%@'>%@</a>", tvMessage.text,NSLocalizedString(@"FOR_DETAILS",@""), 

[self.tinyURLList objectAtIndex:0],[self.tinyURLList objectAtIndex:0]];

如果想在邮件中提供你的App Store应用程序链接,可如下编码:

NSString *pageLink = @"http://mugunthkumar.com/yourapp"; 

NSString *iTunesLink = @"http://link-to-yourapp"; 

NSString *emailBody =

[NSString stringWithFormat:@"%@/n/n<h3>Sent from <a href = '%@'>your app</a> on iPhone. <a href = '%@'>Download</a> yours from AppStore now!</h3>", @"test url", pageLink, iTunesLink];

记住使用HTML格式:

[mailPicker setMessageBody:emailBody isHTML:YES];

 

[self presentModalViewController:mailPicker animated:YES];