简体   繁体   English

使用nodemailer发送电子邮件

[英]Sending emails with nodemailer

I'm trying to send emails from my application using nodemailer. 我正在尝试使用nodemailer从我的应用程序发送电子邮件。

My setup look like this: 我的设置如下所示:

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport ({
  host: 'smtp.companyname.dk',
  secureConnection: true,
  port: 587,
  auth: {
        user: 'support@companyname.dk',
        pass: '****'
  }
}));

var mailOptions = {
    from: 'Olaf <ob@companyname.dk>',
    to: 'john@test.dk',
    subject: 'This is a test ',
        text: 'Hello world ',
        html: '<b>Hello world </b>'
    };

transporter.sendMail(mailOptions, function(error, info){
    if(error){
       console.log(error);
    }else{
    console.log('Message sent: ' + info.response);
    }
});

After running this, it gives me the following error: 运行此命令后,它给了我以下错误:

{ [Error: Hostname/IP doesn't match certificate's altnames: "Host: smtp.companyname.dk. is not in the cert's altnames: DNS:*.mailcloud.dk, DNS:mailcloud.dk"]
  reason: 'Host: smtp.companyname.dk. is not in the cert\'s altnames: DNS:*.mailcloud.dk, DNS:mailcloud.dk',
  host: 'smtp.companyname.dk.',
  cert: 
   { subject: { OU: 'Domain Control Validated', CN: '*.mailcloud.dk' },
     issuer: { O: 'AlphaSSL', CN: 'AlphaSSL CA - G2' },
     subjectaltname: 'DNS:*.mailcloud.dk, DNS:mailcloud.dk',
     infoAccess: { 'CA Issuers - URI': [Object], 'OCSP - URI': [Object] },
     modulus: 'AFFDE405E26382FC1F9A126B51C763A21DCB90228DF9B9BFF466B00DA17BCBDA48E0315E0A6E4E8E0C65B3E5A9BF9A98448DF8006869B913C8D644B78B2176C240AA703DF7C5109F7FC9EDBAE0A69E35E00A81D753990A7DF531CC1F47A778A1CC18271298DBB71E341E846139951D9B119C237BDD9AA6F7D16A62FC5D4D6BEA0288DAC700E5C87E472B75365C522B0342185F16854659EE48637B6124269C4E18CC040344928D115350805EF66738922230349DEC97E2D68D0B4175EA6B63BC161B841C150B7009AAC0E8A06CD34D3912926D98CEBF83F6C187AD08A5045F9868512EFDD478FAA81FA0402F5C44B3FD70F82BA4F102352A01B02B77F0FA096D',
     exponent: '10001',
     valid_from: 'Oct  3 08:22:02 2014 GMT',
     valid_to: 'Dec 21 11:07:29 2017 GMT',
     fingerprint: '35:3A:87:5C:91:8C:B2:7C:4E:87:D1:E3:26:84:57:E5:A1:73:62:F3',
     ext_key_usage: [ '1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2' ],
     serialNumber: '****',
     raw: <Buffer 30 82 04 a7 30 82 03 8f a0 03 02 01 02 02 12 11 21 75 e4 f8 11 73 a4 c5 05 36 5f fe 9b b3 0b 4f 86 30 0d 06 09 2a 86 48 86 f7 0d 01 01 05 05 00 30 2e ... > } }

I've been stuck with this for days now and looked everywhere, so I hope somebody can help me with this (or at least point at the right direction). 我已经被这个问题困扰了好几天,到处都是,所以我希望有人可以帮助我(或者至少指向正确的方向)。

Try adding: 尝试添加:

    tls: {
        rejectUnauthorized: false
    },

To your nodemailer options object. 到您的nodemailer选项对象。

After adding what @Richard Macarthy suggested, and looking up the new error I got back, I figured out that the from email in my mailOptions has to be the same as the email I'm using to send the emails from - which actually makes perfectly sense now. 添加@Richard Macarthy的建议,并查找我返回的新错误后,我发现mailOptions中的from电子邮件必须与我用来发送电子邮件的电子邮件相同-实际上,这很完美现在感觉。

In this case: 在这种情况下:

var mailOptions = {
    from: 'Olaf <support@companyname.dk>',
    to: 'john@test.dk',
    ...
var transporter = nodemailer.createTransport({<br>
host: 'hostSTMP',
secure: false, //disable SSL    
requireTLS: true, //Force TLS
    tls: {
        rejectUnauthorized: false
    },
    port: port, //Port of STMP service
    auth: {
      user: 'user@user.com',
      pass: 'password'
   }
});

rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. rejectUnauthorized:如果为true,则根据提供的CA列表验证服务器证书。 An error event is emitted if verification fails; 如果验证失败,将发出一个错误事件。 Default: true. 默认值:true。

rejectUnauthorized: If false, you're saying "I don't care if I can't verify the server's identity." rejectUnauthorized:如果为false,则表示“我不在乎是否无法验证服务器的身份。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM