iOS使用 [SKPSMTPMessage]傳送郵件
阿新 • • 發佈:2018-12-15
- 使用cocoapods安裝SKPSMTPMessage。
- 簽訂協議
- 程式碼
程式碼部分
@interface ViewController ()<SKPSMTPMessageDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
SKPSMTPMessage *mail = [[SKPSMTPMessage alloc] init];
[mail setSubject:@"測試" ]; // 設定郵件主題
[mail setToEmail:@"[email protected] "]; // 目標郵箱
[mail setFromEmail:@"[email protected]"]; // 傳送者郵箱
[mail setRelayHost:@"smtp.mxhichina.com"]; // 傳送郵件代理伺服器
[mail setRequiresAuth:YES];
[mail setLogin:@"[email protected]"]; // 傳送者郵箱賬號
[mail setPass:@"xxxxxxxxx"]; // 傳送者郵箱密碼
[mail setWantsSecure:YES]; // 需要加密
[mail setDelegate:self];
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:
@"text/plain",kSKPSMTPPartContentTypeKey,
@"This is a test message.",kSKPSMTPPartMessageKey,
@"8bit" ,kSKPSMTPPartContentTransferEncodingKey,nil];
[mail setParts:@[plainPart]]; // 郵件首部欄位、郵件內容格式和傳輸編碼
[mail send];
}
- (void)messageSent:(SKPSMTPMessage *)message
{
NSLog(@"傳送成功%@", message);
}
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
NSLog(@"傳送失敗message - %@\nerror - %@", message, error);
}