2009-09-29 117 views
5

我知道如何通過啓動郵件應用程序在我的應用程序內發送電子郵件,然後返回到我的應用程序......但我希望我的應用程序能夠在不打開郵件應用程序的情況下發送電子郵件。 例如,我會在我的應用程序中有一個按鈕,單擊該按鈕將發送一封電子郵件。然後我會通知用戶電子郵件已發送...iphone應用程序發送電子郵件

有沒有人這樣做?

感謝。

薩米

+0

你如何選擇收件人,還是硬編碼? – Tim 2009-09-29 20:07:17

回答

3

做到這一點,最好的辦法是爲您的應用程序,並在郵件發送一個web服務器。您會傳遞電子郵件的詳細信息,並讓您的服務器代表用戶發送。

+0

如果您無法訪問網絡服務器,則確實存在問題。然後,您必須將郵件排隊以便稍後重試到Web服務器。但是您的應用可能以後不會運行。如果iPhone允許進行一些後臺處理,這將是很好的選擇,例如,如果屏幕被鎖定或幾分鐘內沒有用戶互動。 – mahboudz 2009-09-29 20:47:20

4

你有幾個選擇。您可以使用Apple的MFMailComposeViewController類(見下文),它允許您在您的應用程序中發出消息並將其傳遞到iPhone的郵件,而無需啓動郵件應用程序或離開您的應用程序。您也可以在您的應用中實施SMTP以直接發送電子郵件。您也可以將您的電子郵件發送到網絡服務器,並讓網絡服務器發送出去。最簡單的是第一種方法。缺點是您不知道郵件是否已發出,這取決於網絡是否在運行以及其他因素。當然,如果你使用自己的SMTP代碼,你將不得不處理所有的排隊和重試,以防萬一網絡或服務器不可用,這意味着你的應用必須運行才能做到這一點。

Apple's docs

的MFMailComposeViewController類提供了管理編輯和發送電子郵件的標準接口。您可以使用此視圖控制器在應用程序內部顯示標準電子郵件視圖,並使用初始值(例如主題,電子郵件收件人,正文文本和附件)填充該視圖的字段。用戶可以編輯您指定的初始內容,並選擇發送電子郵件或取消操作。

+0

謝謝,我可能會先嚐試使用MFMailComposeViewController而不啓動郵件應用程序然後... – sami 2009-10-01 12:06:35

9

以下是使用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]]; 
} 
0

添加框架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]; 
     } 
相關問題