Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

sendmail

向地址列表发送电子邮件

语法

sendmail(recipients,subject)
sendmail(recipients,subject,message)
sendmail(recipients,subject,message,attachments)

说明

sendmail(recipients,subject)recipients 发送具有指定 subject 的电子邮件。对于单个收件人,请将 recipients 指定为字符向量或字符串。对于多个收件人,请将 recipients 指定为字符向量元胞数组或字符串数组。subject 必须是字符向量或字符串。

sendmail(recipients,subject,message) 包括指定的 message。如果 message 是字符向量或字符串,则 sendmail 自动在 75 个字符处对文本换行。要强制在消息文本中换行,请使用 10,如示例中所示。如果 message 是字符向量元胞数组或字符串数组,则每个元素代表一行新文本。

sendmail(recipients,subject,message,attachments) 附加 attachments 输入参数中列出的文件。attachments 可以是字符向量、字符向量元胞数组或字符串数组。

示例

将包含两个附件的消息发送到假设的电子邮件地址:

sendmail('user@otherdomain.com',...
         'Test subject','Test message',...
         {'folder/attach1.html','attach2.doc'});

将包含强制换行符(使用 10)的消息发送到假设的电子邮件地址:

sendmail('user@otherdomain.com','New subject', ...
        ['Line1 of message' 10 'Line2 of message' 10 ...
         'Line3 of message' 10 'Line4 of message']);

生成的消息为:

Line1 of message
Line2 of message
Line3 of message
Line4 of message

提示

  • sendmail 函数不支持 HTML 格式的消息。不过,您可以按附件形式发送 HTML 文件。

  • 如果 sendmail 无法根据系统注册表确定您的电子邮件地址或传出 SMTP 邮件服务器,请使用 setpref 函数指定这些设置。例如:

    setpref('Internet','SMTP_Server','my_server.example.com');
    setpref('Internet','E_mail','my_email@example.com');

    要确定用于调用 setpref 的 SMTP 服务器,请检查您的电子邮件应用程序的预设,或咨询您的电子邮件系统管理员。如果您无法轻易地确定服务器名称,请尝试 'mail' 这个常见默认值,例如:

    setpref('Internet','SMTP_Server','mail');
  • 默认情况下,sendmail 函数不支持需要身份验证的电子邮件服务器。要支持这些服务器,请使用以下形式的命令更改您的系统设置并设置 SMTP 用户名和密码的预设:

    props = java.lang.System.getProperties;
    props.setProperty('mail.smtp.auth','true');
    
    setpref('Internet','SMTP_Username','myaddress@example.com');
    setpref('Internet','SMTP_Password','mypassword');
  • 要覆盖默认的字符编码,请按如下方式设置电子邮件字符编码的预设:

    setpref('Internet','E_mail_Charset',encoding); 
    其中 encoding 是一个指定字符编码的字符向量,例如 'SJIS'

备选方法

在装有 Microsoft® Outlook® 的 Windows® 系统上,您可以通过使用 actxserver 访问 COM 服务器,来直接通过 Outlook 发送电子邮件。有关示例,请参阅解决方案 1-RTY6J