最近项目中有需求要通过ABAP开发往外网发邮件,上网查了一些资料,下面是开发过程中整理的一些资料及代码,希望对需要的兄弟有帮助。
ABAP发邮件需要两大块:一是Basis配置邮件服务器,二是ABAP写发送邮件代码,下面分别介绍一下:
一、Step By Step配置SAP邮件服务器,通过配置,不仅能向Sap User 发送邮件,也能发送外部邮件,具体步骤如下:
1、RZ10配置
Step1:运行事务码RZ10,显示如图1所示界面,分别进行图1中1、2、3步操作。
图1
Step2:进入修改界面,如下图2所示,点图2中红色箭头所指示的“参数”按钮。
图2
Step3:新建参数icm/server_port_1 赋值为“PROT=SMTP,PORT=25”。 默认情况下已经有一个参数文件icm/server_port_0 = PORT=HTTP,PORT=XXXX(每个服务可能不一样),那么这里的<*>就是要根据你的参数文件的具体情况,如果已经有了_0,这里你就需要设成_1,以此类推,PORT一般设置成25。 这里还有一个选项是TIMEOUT可以设定等待邮件服务器回复时间的最大值。 这里我的参数文件时:icm/server_port_1 = PROT=SMTP,PORT=25 如下图3中所示
图3
Step4:返回到图2界面,并点击图2中红色箭头所指示的“参数”按钮。新建参数is/SMTP/virt_host_0 赋值为“ *:25;”。is/SMTP/virt_host_<*> = <host>:<port>,<port>,...; 定义虚拟邮件主机,主要用来接收邮件的,_<*>的设置同上。要注意,参数值最后是有一个分号的。这里我的参数文件是is/SMTP/virt_host_0 = *:25;完成后保存,激活。 配置完需要重启服务,参数才能生效。如下图4所示:
图4
2、SU01配置。
Step1: 对于每一个client,需要创建一个用户作为邮件的接收者,创建了用户liuxy,用户类型设置为service,并给该用户赋profile S_A.SCON。如下图5,图6所示
图5
图6
Step2:配置Email信息,如下图7所示:
图7
3、SICF配置。
执行事务码SICF进入如下图8界面,点图中执行按钮,进入图9界面。
图8
Step2:双击图9中红色框内的SAPconnect,进入图10界面
图9
Step3:下图中Host data 主机数据,对于 Profile Parameter Number(文件参数编号),输入 "is/STMP/virt_host_<*>"中的<*>的值,一般可能是0,如果不存在的话,下面会有提示的。
图10
Step4:登陆数据(logon data),客户端:当前客户端编号;用户:前面su01中创建的service用户;语言:中文,如下图11所示。
图11
Step5:处理器清单(Handler List),输入CL_SMTP_EXT_SAPCONNECT,如下图12所示
图12
Step6:完成以上配置后,返回到图9界面,保存激活。
4、SCOT配置。
图13
Step1: 菜单栏 设置----Default domain,此处我写的是公司内网的邮件服务器。 这个邮件的默认域名比如sap.com,那么如果在你发送邮件的时候收件人地址如果只写test的话,系统会自动加上@sap.com,如果收件人地址是全的话,这个domain不维护关系也不大.
Step2: 维护SMTP节点:视图----节点Nodes—打开节点或者双击上上图SMTP后弹出对话框,维护 以下信息勾上 "Node in user".在MAIL HOST和MAIL PORT下面,指定发送邮件服务器的地址,比如如果是163的话,就应该是smtp.163.com。这里MAIL HOST填写我公司邮件服务器地址,MAIL PORT填写25选中Internet的Set,弹出新对话框,指定接收地址的地址区域,一般用*表示所有邮件都用SMTP来发送。其他信息用默认。
这里SAPconnect的信息可以有两种方式显示的,如果双击SMTP节点弹出的是JOB的信息的话,选择菜单中SYSTEM STATUS,切换到为另一种显示方式即可。
Step3: Send job,选择菜单中的视图View-->职务Jobs,可以检查是否已经有Jobs被调度了 选择Jobs->Create,并指定Job名称,点执行按钮,选择SAP&CONNECTALL变式,并选择Schedule Job。选择 Schedule periodiacally定期计划,指定时间间隔,比如10分钟,选择创建。到这里,基本配置成功了。
Step4: 可以在SBWP中,选择发送邮件,来测试配置是否成功,输入收件人的邮件地址,点击发送。如果配置成功的话,收一下邮件,应该收到了来自登陆SAP GUI的账号中配置的邮件地址的邮件了。
二、ABAP代码,以下是从网上copy代码
DATA GV_METHOD1 LIKE SY-UCOMM.
DATA GS_USER LIKE SOUDNAMEI1.
DATA GS_USER_DATA LIKE SOUDATAI1.
DATA GV_OWNER LIKE SOUD-USRNAM.
DATA GT_RECEIPIENTS LIKE SOOS1 OCCURS 0 WITH HEADER LINE.
DATA GS_DOCUMENT LIKE SOOD4 .
DATA GS_HEADER2 LIKE SOOD2.
DATA GS_FOLMAM LIKE SOFM2.
DATA GT_OBJCNT LIKE SOLI OCCURS 0 WITH HEADER LINE.
DATA GT_OBJHEAD LIKE SOLI OCCURS 0 WITH HEADER LINE.
DATA GT_OBJPARA LIKE SELC OCCURS 0 WITH HEADER LINE.
DATA GT_OBJPARB LIKE SOOP1 OCCURS 0 WITH HEADER LINE.
DATA GT_ATTACHMENTS LIKE SOOD5 OCCURS 0 WITH HEADER LINE.
DATA GT_REFERENCES LIKE SOXRL OCCURS 0 WITH HEADER LINE.
DATA GS_RECIVER LIKE SOOS6 .
DATA GV_AUTHORITY LIKE SOFA-USRACC.
DATA GS_REF_DOCUMENT LIKE SOOD4.
DATA GS_NEW_PARENT LIKE SOODK.
DATA: BEGIN OF GT_FILES OCCURS 10 ,
TEXT(4096) TYPE C,
END OF GT_FILES.
DATA : GV_FOLD_NUMBER(12) TYPE C,
GV_FOLD_YR(2) TYPE C,
GV_FOLD_TYPE(3) TYPE C.
DATA: GS_FOLDER_ID LIKE SOODK,
GS_ORDER_ID LIKE SOODK.
DATA: GV_MAIL_TITLE(50).
DATA: GT_RECEIVERS LIKE SOOS1 OCCURS 0 WITH HEADER LINE.
INITIALIZATION.
START-OF-SELECTION.
GV_MAIL_TITLE = '这是邮件标题'.
** 发件人信息 edit
PERFORM SO_USER_READ.
**邮件信息、属性定义
PERFORM DOCUMENT_REPOSITORY.
**附件生成
PERFORM CREAT_ATTACHMENTS.
**send mail
PERFORM SEND.
*&---------------------------------------------------------------------*
*& Form SO_USER_READ
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM SO_USER_READ .
GS_USER-SAPNAME = SY-UNAME.
CALL FUNCTION 'SO_USER_READ_API1'
EXPORTING
USER = GS_USER
IMPORTING
USER_DATA = GS_USER_DATA
EXCEPTIONS
USER_NOT_EXIST = 1
PARAMETER_ERROR = 2
X_ERROR = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
GV_FOLD_TYPE = GS_USER_DATA-OUTBOXFOL+0(3).
GV_FOLD_YR = GS_USER_DATA-OUTBOXFOL+3(2).
GV_FOLD_NUMBER = GS_USER_DATA-OUTBOXFOL+5(12).
CLEAR: GT_FILES[], GT_FILES.
REFRESH : GT_OBJCNT,
GT_OBJHEAD,
GT_OBJPARA,
GT_OBJPARB,
GT_RECEIPIENTS,
GT_ATTACHMENTS,
GT_REFERENCES,
GT_FILES.
CLEAR : GS_DOCUMENT,
GS_HEADER2,
GT_OBJCNT,
GT_OBJHEAD,
GT_OBJPARA,
GT_OBJPARB,
GT_RECEIPIENTS,
GT_ATTACHMENTS,
GT_REFERENCES,
GT_FILES.
ENDFORM. " SO_USER_READ
*&---------------------------------------------------------------------*
*& Form DOCUMENT_REPOSITORY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DOCUMENT_REPOSITORY .
GV_METHOD1 = 'SAVE'.
GS_DOCUMENT-FOLTP = GV_FOLD_TYPE.
GS_DOCUMENT-FOLYR = GV_FOLD_YR.
GS_DOCUMENT-FOLNO = GV_FOLD_NUMBER.
GS_DOCUMENT-OBJTP = GS_USER_DATA-OBJECT_TYP.
*g_document-OBJYR = '27'.
*g_document-OBJNO = '000000002365'.
*g_document-OBJNAM = 'MESSAGE'.
GS_DOCUMENT-OBJDES = 'mail of sap by program'.
GS_DOCUMENT-FOLRG = 'O'.
*g_document-okcode = 'CHNG'.
GS_DOCUMENT-OBJLEN = '0'.
* g_document-file_ext = 'TXT'.
GS_HEADER2-OBJDES = GV_MAIL_TITLE.
* g_header-file_ext = 'TXT'.
CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'
EXPORTING
METHOD = GV_METHOD1
OFFICE_USER = SY-UNAME
REF_DOCUMENT = GS_REF_DOCUMENT
NEW_PARENT = GS_NEW_PARENT
IMPORTING
AUTHORITY = GV_AUTHORITY
TABLES
OBJCONT = GT_OBJCNT
OBJHEAD = GT_OBJHEAD
OBJPARA = GT_OBJPARA
OBJPARB = GT_OBJPARB
RECIPIENTS = GT_RECEIPIENTS
ATTACHMENTS = GT_ATTACHMENTS
REFERENCES = GT_REFERENCES
FILES = GT_FILES
CHANGING
DOCUMENT = GS_DOCUMENT
HEADER_DATA = GS_HEADER2.
ENDFORM. " DOCUMENT_REPOSITORY
*&---------------------------------------------------------------------*
*& Form CREAT_ATTACHMENTS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM CREAT_ATTACHMENTS .
* File from the pc to send..
GV_METHOD1 = 'ATTCREATEFROMPC'.
* GV_SAVE_PATHT = P_FILE.
GT_FILES-TEXT = 'C:\20150318101349.XLS'.
APPEND GT_FILES.
CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'
EXPORTING
METHOD = GV_METHOD1
OFFICE_USER = GV_OWNER
REF_DOCUMENT = GS_REF_DOCUMENT
NEW_PARENT = GS_NEW_PARENT
IMPORTING
AUTHORITY = GV_AUTHORITY
TABLES
OBJCONT = GT_OBJCNT
OBJHEAD = GT_OBJHEAD
OBJPARA = GT_OBJPARA
OBJPARB = GT_OBJPARB
RECIPIENTS = GT_RECEIPIENTS
ATTACHMENTS = GT_ATTACHMENTS
REFERENCES = GT_REFERENCES
FILES = GT_FILES
CHANGING
DOCUMENT = GS_DOCUMENT
HEADER_DATA = GS_HEADER2.
ENDFORM. " CREAT_ATTACHMENTS
*&---------------------------------------------------------------------*
*& Form SEND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM SEND .
CLEAR: GS_FOLDER_ID,GS_ORDER_ID.
GS_FOLDER_ID-OBJTP = GS_DOCUMENT-FOLTP .
GS_FOLDER_ID-OBJYR = GS_DOCUMENT-FOLYR.
GS_FOLDER_ID-OBJNO = GS_DOCUMENT-FOLNO.
GS_ORDER_ID-OBJTP = GS_DOCUMENT-OBJTP.
GS_ORDER_ID-OBJYR = GS_DOCUMENT-OBJYR.
GS_ORDER_ID-OBJNO = GS_DOCUMENT-OBJNO.
*收件人信息可以添加多个收件人-----------------------------------
REFRESH GT_RECEIVERS.
GT_RECEIVERS-RECESC = 'U'.
GT_RECEIVERS-SNDEX = 'X'.
GT_RECEIVERS-RECEXTNAM = '[email protected]'.
APPEND GT_RECEIVERS.
CALL FUNCTION 'SO_OBJECT_SEND'
EXPORTING
FOLDER_ID = GS_FOLDER_ID
OBJECT_ID = GS_ORDER_ID
TABLES
OBJCONT = GT_OBJCNT
OBJHEAD = GT_OBJHEAD
OBJPARA = GT_OBJPARA
OBJPARB = GT_OBJPARB
RECEIVERS = GT_RECEIVERS
PACKING_LIST = GT_ATTACHMENTS
EXCEPTIONS
ACTIVE_USER_NOT_EXIST = 1
COMMUNICATION_FAILURE = 2
COMPONENT_NOT_AVAILABLE = 3
FOLDER_NOT_EXIST = 4
FOLDER_NO_AUTHORIZATION = 5
FORWARDER_NOT_EXIST = 6
NOTE_NOT_EXIST = 7
OBJECT_NOT_EXIST = 8
OBJECT_NOT_SENT = 9
OBJECT_NO_AUTHORIZATION = 10
OBJECT_TYPE_NOT_EXIST = 11
OPERATION_NO_AUTHORIZATION = 12
OWNER_NOT_EXIST = 13
PARAMETER_ERROR = 14
SUBSTITUTE_NOT_ACTIVE = 15
SUBSTITUTE_NOT_DEFINED = 16
SYSTEM_FAILURE = 17
TOO_MUCH_RECEIVERS = 18
USER_NOT_EXIST = 19
X_ERROR = 20
OTHERS = 21.
COMMIT WORK.
******直接发送邮件的代码********
SUBMIT RSCONN01 WITH MODE ='INT'
WITH OUTPUT = 'X'
AND RETURN.
ENDFORM. " SEND