iPhone应用程序发送电子邮件

时间:2009-09-29 20:04:04

标签: iphone email

我知道如何通过启动邮件应用程序然后返回我的应用程序在我的应用程序中发送电子邮件...但我希望我的应用程序能够在不打开邮件应用程序的情况下发送电子邮件。 例如,我的应用程序中有一个按钮,单击该按钮会发送一封电子邮件。然后我会通知用户该电子邮件已发送...

有人这样做过吗?

感谢。

萨米

4 个答案:

答案 0 :(得分:9)

以下是使用MFMailComposeViewController发送电子邮件的示例代码。

-(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:@"first@example.com"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

[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:first@example.com?cc=second@example.com,third@example.com&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]];
}

答案 1 :(得分:4)

你有几个选择。您可以使用Apple的MFMailComposeViewController类(见下文),它允许您在应用程序中发送消息并将其传递给iPhone的Mail,而无需启动Mail应用程序或离开您的应用程序。您还可以在应用中实施SMTP以直接发送电子邮件。您也可以将电子邮件移交给网络服务器,并让网络服务器将其发送出去。最简单的是第一种方式。缺点是您不知道消息是否已发送,这取决于网络是否正常运行以及其他因素。当然,如果您使用自己的SMTP代码,则必须处理所有排队和重试,以防网络或服务器不可用,这意味着您的应用必须运行才能执行此操作。

来自Apple's docs

MFMailComposeViewController类提供管理编辑和发送电子邮件的标准界面。您可以使用此视图控制器在应用程序内显示标准电子邮件视图,并使用初始值填充该视图的字段,例如主题,电子邮件收件人,正文和附件。用户可以编辑您指定的初始内容,并选择发送电子邮件或取消操作。

答案 2 :(得分:3)

执行此操作的最佳方法是为您的应用程序提供发送邮件的网络服务器。您将传递电子邮件的详细信息,并让您的服务器代表用户发送。

答案 3 :(得分:0)

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