2009-09-29 10 views
5

Uygulamamda Mail uygulamasını başlatarak ve uygulamanıza dönerek e-posta göndermeyi biliyorum ... ancak uygulamanızı posta uygulamasını açmadan e-posta gönderebilmek için istiyorum. Örnek olarak, bu düğmeyi tıklatarak bu düğmeyi tıklayarak bir düğmem olurdu. Daha sonra kullanıcıya e-postanın gönderildiğini bildireceğim ...iphone app e-posta gönder

Bunu yapan var mı?

Teşekkürler.

Sami

+0

Alıcıları nasıl seçersiniz, yoksa kodlanmış mı? – Tim

cevap

3

Bunu yapmanın en iyi yolu, posta gönderme yapar uygulamanız için bir web sunucusu sahip olmaktır. E-postanın ayrıntılarını geçip sunucunuzun onu kullanıcı adına göndermesini sağladınız.

+0

Web sunucusuna erişemiyorsanız sorun yaşarsınız. Ardından, web sunucusuna yeniden denemek için iletiyi sıraya almanız gerekir. Ancak uygulamanız daha sonra yayınlanmayabilir. Eğer iPhone kilitliyse veya birkaç dakika boyunca kullanıcı etkileşimi olmadıysa, iPhone'un arka plan işlemeye izin vermesi güzel olurdu. – mahboudz

4

Birkaç seçeneğiniz var. Uygulamanızda bir mesaj oluşturmanıza ve Mail uygulamasını başlatmanıza veya sizinkini bırakmanıza gerek kalmadan iPhone'un Postasına iletmenizi sağlayan Apple'ın MFMailComposeViewController sınıfını (aşağıya bakın) kullanabilirsiniz. Doğrudan e-posta göndermek için uygulamanızda SMTP'yi de uygulayabilirsiniz. Ayrıca e-postanızı bir web sunucusuna aktarabilir ve web sunucusunu gönderebilirsiniz. En kolayı ilk yoldur. Bunun dezavantajı, mesajın gönderilip gönderilmediğini, ağın çalışıp çalışmadığını ve diğer faktörlere bağlı olup olmadığını gerçekten bilmemenizdir. Elbette, kendi SMTP kodunuzla giderseniz, ağın veya sunucunun kullanılamaması durumunda, tüm kuyruk işlemlerini gerçekleştirmeniz ve yeniden denemeniz gerekir. Bu, uygulamanızın bunu yapmak için yayınlanmak zorunda olduğu anlamına gelir. Apple's docs itibaren

:

MFMailComposeViewController sınıf düzenleme ve bir e-posta mesajı göndererek yöneten bir standart arayüz sağlar. Uygulamanızdaki standart bir e-posta görünümünü görüntülemek ve bu görünümün alanlarını konu, e-posta alıcıları, gövde metni ve ekler gibi başlangıç ​​değerleriyle doldurmak için bu görünüm denetleyicisini kullanabilirsiniz. Kullanıcı belirttiğiniz ilk içeriği düzenleyebilir ve e-postayı göndermeyi veya işlemi iptal etmeyi seçebilir.

+0

teşekkürler, muhtemelen posta uygulamasını başlatmadan önce MFMailComposeViewController kullanmayı deneyin. – sami

9

MFMailComposeViewController kullanarak e-posta göndermek için örnek bir kod. buildphases içinde

-(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

Ekleme çerçevesi MessageUI.framework

ViewController.h dosya

#import <MessageUI/MessageUI.h> 

    @interface ViewController() <MFMailComposeViewControllerDelegate> 

ViewController.m dosyası

-(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]; 
     }