我知道如何通过启动邮件应用程序在我的应用程序内发送电子邮件,然后返回到我的应用程序......但我希望我的应用程序能够在不打开邮件应用程序的情况下发送电子邮件。 例如,我会在我的应用程序中有一个按钮,单击该按钮将发送一封电子邮件。然后我会通知用户电子邮件已发送...iphone应用程序发送电子邮件
有没有人这样做?
感谢。
萨米
我知道如何通过启动邮件应用程序在我的应用程序内发送电子邮件,然后返回到我的应用程序......但我希望我的应用程序能够在不打开邮件应用程序的情况下发送电子邮件。 例如,我会在我的应用程序中有一个按钮,单击该按钮将发送一封电子邮件。然后我会通知用户电子邮件已发送...iphone应用程序发送电子邮件
有没有人这样做?
感谢。
萨米
做到这一点,最好的办法是为您的应用程序,并在邮件发送一个web服务器。您会传递电子邮件的详细信息,并让您的服务器代表用户发送。
如果您无法访问网络服务器,则确实存在问题。然后,您必须将邮件排队以便稍后重试到Web服务器。但是您的应用可能以后不会运行。如果iPhone允许进行一些后台处理,这将是很好的选择,例如,如果屏幕被锁定或几分钟内没有用户互动。 – mahboudz 2009-09-29 20:47:20
你有几个选择。您可以使用Apple的MFMailComposeViewController类(见下文),它允许您在您的应用程序中发出消息并将其传递到iPhone的邮件,而无需启动邮件应用程序或离开您的应用程序。您也可以在您的应用中实施SMTP以直接发送电子邮件。您也可以将您的电子邮件发送到网络服务器,并让网络服务器发送出去。最简单的是第一种方法。缺点是您不知道邮件是否已发出,这取决于网络是否在运行以及其他因素。当然,如果你使用自己的SMTP代码,你将不得不处理所有的排队和重试,以防万一网络或服务器不可用,这意味着你的应用必须运行才能做到这一点。
的MFMailComposeViewController类提供了管理编辑和发送电子邮件的标准接口。您可以使用此视图控制器在应用程序内部显示标准电子邮件视图,并使用初始值(例如主题,电子邮件收件人,正文文本和附件)填充该视图的字段。用户可以编辑您指定的初始内容,并选择发送电子邮件或取消操作。
谢谢,我可能会先尝试使用MFMailComposeViewController而不启动邮件应用程序然后... – sami 2009-10-01 12:06:35
以下是使用MFMailComposeViewController发送电子邮件的示例代码。在buildphases
-(IBAction)showPicker:(id)sender {
// This sample can run on devices running iPhone OS 2.0 or later
// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later.
// So, we must verify the existence of the above class and provide a workaround for devices running
// earlier versions of the iPhone OS.
// We display an email composition interface if MFMailComposeViewController exists and the device can send emails.
// We launch the Mail application on the device, otherwise.
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
-(void)displayComposerSheet {
// Displays an email composition interface inside the application. Populates all the Mail fields.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];
// Fill out the email body text
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
message.text = @"Result: canceled";
break;
case MFMailComposeResultSaved:
message.text = @"Result: saved";
break;
case MFMailComposeResultSent:
message.text = @"Result: sent";
break;
case MFMailComposeResultFailed:
message.text = @"Result: failed";
break;
default:
message.text = @"Result: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
-(void)launchMailAppOnDevice {
// Launches the Mail application on the device.
NSString *recipients = @"mailto:[email protected][email protected],[email protected]&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
添加框架MessageUI.framework
ViewController.h文件
#import <MessageUI/MessageUI.h>
@interface ViewController() <MFMailComposeViewControllerDelegate>
ViewController.m文件
-(IBAction)emailButtonClicked:(id)sender{
MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init];
if (mailComposer !=nil) {
mailComposer.mailComposeDelegate = self;
NSString *emailBody = @"Write the text here........";
[mailComposer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
你如何选择收件人,还是硬编码? – Tim 2009-09-29 20:07:17