【问题标题】:iphone app send emailiphone应用程序发送电子邮件
【发布时间】:2010-12-02 10:50:00
【问题描述】:

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

有人这样做过吗?

谢谢。

萨米

【问题讨论】:

  • 如何选择收件人,还是硬编码?

标签: iphone email


【解决方案1】:

这是使用 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]];
}

【讨论】:

    【解决方案2】:

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

    来自Apple's docs

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

    【讨论】:

    • 谢谢,我可能会先尝试使用 MFMailComposeViewController 而不启动邮件应用程序然后...
    【解决方案3】:

    最好的方法是为您的应用程序设置一个网络服务器来发送邮件。您将传递电子邮件的详细信息并让您的服务器代表用户发送它。

    【讨论】:

    • 如果您无法访问网络服务器,您确实有问题。然后,您必须将消息排队以便稍后重试到网络服务器。但是您的应用程序可能稍后不会运行。如果 iPhone 允许进行一些后台处理(例如,如果屏幕被锁定或几分钟没有用户交互),那就太好了。
    【解决方案4】:

    在构建阶段添加框架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];
             }
    

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多