从终端控制台手动发送邮件?

时间:2016-01-11 15:49:55

标签: intersystems-cache objectscript

我们有一个构建和发送邮件消息的类。我想做一些更改,但在此之前,我想测试一些事情(比如URL的显示方式)。我试图从控制台手动发送消息,但我遇到了一些错误。以下是该类的SendMessage方法:

Method SendMessage(pSubject As %String, pMessage As %String, pEmailAddresses) As %Status
{
    set tSC=$$$OK
    set tMailMessage=##class(%Net.MailMessage).%New()
    do tMailMessage.To.Insert($PIECE(pEmailAddresses,",",1))
    for tI=2:1:$LENGTH(pEmailAddresses,",") {
        do tMailMessage.Cc.Insert($PIECE(pEmailAddresses,",",tI))
    }

    set tMailMessage.Subject=pSubject
    set tMailMessage.Charset="iso-8859-1"
    set tSC=tMailMessage.TextData.Write(pMessage)
    quit:'tSC
    Set tSC1=..Adapter.SendMail(tMailMessage)
    if 'tSC1 {
        //Log warning about being unable to send mail.
        do $SYSTEM.Status.DecomposeStatus(tSC1,.err)
        $$$LOGWARNING("Could not send email: "_err(err))
        kill err
    }

    quit tSC
}

从终端,我可以实例化MailMessage类并设置正文数据,但是当我尝试发送时,我收到错误:

USER>set tMailMessage=##class(%Net.MailMessage).%New()

USER>do tMailMessage.To.Insert("me@email.com")

USER>set tSC=tMailMessage.TextData.Write("This is a URL test http://www.google.com, thank you")

USER>set tMailMessage.Subject="This is a test"

USER>set tMailMessage.Charset="iso-8859-1"

USER>set tSC1=..Adapter.SendMail(tMailMessage)

SET tSC1=..Adapter.SendMail(tMailMessage)
^
<NO CURRENT OBJECT>

正如您所看到的,当我尝试SendMail时,它会告诉我NO CURRENT OBJECT

更新 我在课程顶部注意到这些行:

Parameter ADAPTER = "EnsLib.EMail.OutboundAdapter";

Property Adapter As EnsLib.EMail.OutboundAdapter;

所以我尝试USER>set tSC1=EnsLib.EMail.OutboundAdapter.SendMail(tMailMessage),但结果是<UNDEFINED> *EnsLib

1 个答案:

答案 0 :(得分:2)

我认为您正在使用Ensemble Service。但是为了测试,你不应该使用那些类。要发送电子邮件,您可以直接使用%Net.SMTP。 所以,而不是..Adapter.SendMail使用此代码

set s=##class(%Net.SMTP).%New()
set s.smtpserver="SMTP server name"

#; if SMTP server needs auth
set auth=##class(%Net.Authenticator).%New() ; use default authentication list
set auth.UserName="myUser"
set auth.Password="myPassword"
set s.authenticator=auth

set status=s.Send(tMailMessage)
if $$$ISERR(status) do $system.OBJ.DisplayError(status)