Skip to content

informat.email 邮件相关操作

概述

使用informat.email对象发送邮件

send

发送邮件

javascript
informat.email.send(server, message);

TIP

当邮件地址错误,或者是服务器连接失败都会导致邮件发送失败,失败后会抛出异常

参数类型描述
serverEmailServer发送邮件的服务器
messageEmailMessage邮件的内容

示例

javascript
const server = {
    host: 'smtp.gmail.com',
    port: 465,
    ssl: true,
    auth: true,
    account: 'account@informat.cn',
    password: 'password',
    protocol: 'smtp'
}
const textMessage = {
    recipients: [
        { address: 'to1@informat.cn', 'personal': 'sky', type: 'TO' },
        { address: 'cc1@informat.cn', 'personal': 'ginko', type: 'CC' },//抄送
    ],
    subject: '邮件的标题',
    personal: '发件人昵称',
    text: '纯文本邮件内容',//纯文本的内容
}
const richMessage = {
    recipients: [
        { address: 'to1@informat.cn', 'personal': 'sky', type: 'TO' },
        { address: 'cc1@informat.cn', 'personal': 'ginko', type: 'CC' },//抄送
    ],
    subject: '邮件的标题',
    personal: '发件人昵称',
    multiparts: [
        {
            type: 'text',
            content: '纯文本内容'
        },
        {
            type: 'content',
            contentType: "text/html;charset=utf-8",
            content: '<div style="color:red">富文本内容</div><img src="cid:image1"/>'
        },
        {
            type: 'attachment',
            contentId: '<image1>',
            disposition: 'inline',
            filename: 'image1.jpg',
            filepath: "logo.jpg"
        },//显示在富文本中的图片
        {
            type: 'attachment',
            filename: 'excel.xlsx',
            filepath: "test.xlsx"
        },//邮件的附件(本地文件)
        {
            type: 'attachment',
            filename: 'excel.xlsx',
            storageFilepath: "test.xlsx"
        }//邮件的附件(共享存储文件)
    ]
}
//
try {
    informat.email.send(server, textMessage);
    informat.email.send(server, richMessage);
} catch (e) {
    console.log('发送失败');
}

sendWithSystemServer

使用系统邮箱发送邮件

javascript
informat.email.sendWithSystemServer(message);

TIP

当邮件地址错误,或者是服务器连接失败都会导致邮件发送失败,失败后会抛出异常

参数类型描述
messageEmailMessage邮件的内容

示例

js
const textMessage = {
    recipients: [
        { address: 'to1@informat.cn', 'personal': 'sky', type: 'TO' },
        { address: 'cc1@informat.cn', 'personal': 'ginko', type: 'CC' },//抄送
    ],
    subject: '邮件的标题',
    personal: '发件人昵称',
    text: '纯文本邮件内容',//纯文本的内容
}
informate.email.sendWithSystemServer(textMessage);