使用Node.js服务器从iOS应用程序发送文本(电子邮件)

时间:2015-01-29 12:31:45

标签: ios node.js email

我正在尝试使用Node.js服务器从我的应用发送邮件。我想将电子邮件的“发件人”和“内容”发送到服务器,然后他会将电子邮件发送给此人。

我发现Nodemailer用于发送电子邮件,但我如何将数据(从内容发送到内容)发送到服务器?我正在Swift中编写我的应用程序。

编辑:
这是我的应用程序代码:

func post(params: Dictionary<String,String>, url:String) {
    var request = NSMutableURLRequest(URL: NSURL(string: url)!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"

    var err:NSError?
    var jsonObject = NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted, error: &err)

    request.HTTPBody = jsonObject
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    var task = session.dataTaskWithRequest(request, completionHandler: { (data,response,error)-> Void in

        println(response)

    })
    task.resume()

}

我使用Email,From,To和Content等参数调用此函数,并将它们发送到Nodejs服务器。 服务器处理JSON对象(发送电子邮件)并返回“成功”或“失败”的正确代码是什么?如何在服务器代码中使用JSON对象?

2 个答案:

答案 0 :(得分:2)

in Node 

/*Config mailer*/
    var nodemailer = require('nodemailer');
    var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'xxx@gmail.com',
        pass: 'yyyy'
      }
    });


Create Function 

    function MSGEmail(MSG) {
      // send the message and get a callback with an error or details of the message that was sent
      transporter.sendMail({
        from: 'sender@address',
        to: 'xx@gmail.com',
        cc: 'yy@gmail.com',
        subject: 'Auto Email From server',
        text: MSG
      });
    }



    //Register Event

    client.on('SEND_MSG', function(data, callback) {
      sendEmail(data); //handle msg and pass to funcation
      callback();
    });

    // iOS (ObjC)


    [APPCONTEXT.socketIOHandler.socketIO sendEvent: KEY_SEND_MSG withData: dictionary
      andAcknowledge: ^ (id argsData) {
        if (argsData) {}];

答案 1 :(得分:1)

Nodemailer有一个API,可以向您展示如何处理他们的模块, 您所要做的就是将这些细节(从,到内容)提供给服务器端并以这种方式处理它,

var nodemailer = require('nodemailer');
// in case your using gmail as your mail serivce.
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'sender@gmail.com',
        pass: 'password'
    }
});
transporter.sendMail({
    from: 'sender@address',
    to: 'receiver@address',
    subject: 'hello',
    text: 'hello world!'
});