邮件配置
服务器配置
SMTP服务
可通过SMICM新建,该方式配置参数为临时的,当重启服务器时配置的参数会被删除。也可通过RZ10配置完毕重启服务器,该方式为永久配置的,推荐RZ10配置参数。
端口号一般情况下为25000,有的端口是 0,也是可以的,看是否活动的,如果没 SMTP 服务就要去创建
SMICM配置:
创建步骤如下:
- 确保新建服务是活动状态
- RZ10配置:
- 输入参数 点击保存
- 重启服务器
- 最后查看SMTP服务是否启动(活动状态)
配置STMP节点
事务代码SCOT新建STMP节点时可以新建多个STMP,系统以最新建或者最新修改的为准
新建STMP节点步骤如下:
维护邮件主机和端口号,其余默认,输入的mail host可预先ping一下,看是否通畅(该方法不是万能的,有些邮箱主机PING不通,但是却能正常收发邮件)
双击新建的STMP
- 点击settings
- 输入发送邮箱的用户里维护的邮箱和密码
- 设置缺省域,取@后面的邮箱地址,点击保存
- 如果不需要读取回执,则勾选此项
- 上述配置完毕之后,可测试发送邮件
- 输入接受邮件地址
- 点击start,其余默认即可
邮件发送方式
函数方式
REPORT zys089.
CONSTANTS:
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab, "CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf. "CL_ABAP_CHAR_UTILITIES=>CR_LF
DATA: i_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
i_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE, "receivers
i_contents_txt LIKE solisti1 OCCURS 0 WITH HEADER LINE, "ASCII contents of object and attachments
i_contents_hex LIKE solix OCCURS 0 WITH HEADER LINE, "Binary contents of object and attachments
i_contents_bin LIKE solisti1 OCCURS 0 WITH HEADER LINE,
i_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE, "attachments name
w_doc_data LIKE sodocchgi1.
DATA: c_mimetype TYPE char64 VALUE 'APPLICATION/MSEXCEL;charset=utf-16le'.
DATA: v_xattach TYPE xstring.
DATA: lv_string TYPE string.
DATA: tab_lines LIKE sy-tabix,
w_sent_all(1) TYPE c,
w_error TYPE sy-subrc.
DATA: i_spfli LIKE STANDARD TABLE OF spfli WITH HEADER LINE.
***********************************************************************
START-OF-SELECTION.
PERFORM get_spfli_data.
***define mail main context****
PERFORM define_mail_header.
***dfine attachment*******************
PERFORM define_mail_attachment.
***get mail receiver*****************
PERFORM get_receivers.
*Send email message, although is not sent from SAP until mail send
*program has been executed(rsconn01)
PERFORM send_email_message.
*&---------------------------------------------------------------------*
*& Form GET_SPFLI_DATA
*&---------------------------------------------------------------------*
FORM get_spfli_data .
SELECT * FROM spfli
INTO CORRESPONDING FIELDS OF TABLE i_spfli.
ENDFORM. " GET_SPFLI_DATA
*&---------------------------------------------------------------------*
*& Form DEFINE_MAIL_HEADER
*&---------------------------------------------------------------------*
FORM define_mail_header .
DATA: tmp_date LIKE sy-datum.
DATA: tmp_char(30).
tmp_date = sy-datum.
CONCATENATE 'Overdue Batch ' sy-datum INTO tmp_char.
********define subject and priority************
w_doc_data-obj_descr = 'Hello'. "Email Subject.
w_doc_data-priority = 1. "mail PRIORITY¡A 1 is highest
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'OFFER'. "Name of document..
w_doc_data-sensitivty = 'O'. "Document sensitivity.
********mail main context***********************
i_contents_txt = 'Dear Mr and Miss:'.
APPEND i_contents_txt.
i_contents_txt = ' '.
APPEND i_contents_txt.
i_contents_txt = ' Please check attachment in time!'.
APPEND i_contents_txt.
i_contents_txt = ' This Email is sent by systme, Do not Return the Email.'.
APPEND i_contents_txt.
i_contents_txt =' If you have any problem, please contact with ERP.'.
APPEND i_contents_txt.
i_contents_txt = ' '.
APPEND i_contents_txt.
i_contents_txt = tmp_date.
APPEND i_contents_txt.
DESCRIBE TABLE i_contents_txt LINES tab_lines.
READ TABLE i_contents_txt INDEX tab_lines.
w_doc_data-doc_size = ( tab_lines - 1 ) * 255 + strlen( i_contents_txt ).
********count lines by man context*************
CLEAR i_packing_list-transf_bin.
i_packing_list-transf_bin = space. "ASCII format/binary format
i_packing_list-head_start = 1.
i_packing_list-head_num = 0.
i_packing_list-body_start = 1.
i_packing_list-body_num = tab_lines.
i_packing_list-doc_type = 'RAW'.
APPEND i_packing_list.
ENDFORM. " DEFINE_MAIL_HEADER
*&---------------------------------------------------------------------*
*& Form DEFINE_MAIL_ATTACHMENT
*&---------------------------------------------------------------------*
FORM define_mail_attachment .
LOOP AT i_spfli.
CONCATENATE lv_string
i_spfli-carrid gc_tab
i_spfli-connid gc_tab
i_spfli-countryfr gc_tab
i_spfli-cityfrom gc_tab
i_spfli-cityfrom gc_tab
i_spfli-countryto gc_tab
i_spfli-cityto gc_tab
i_spfli-airpto gc_crlf
INTO lv_string.
ENDLOOP.
* Convert string to xstring type
* 'APPLICATION/MSEXCEL;charset=utf-16le'
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_string
mimetype = c_mimetype
IMPORTING
buffer = v_xattach
EXCEPTIONS
failed = 1
OTHERS = 2.
* Add the file header for utf-16le. .
IF sy-subrc = 0.
CONCATENATE cl_abap_char_utilities=>byte_order_mark_little
v_xattach INTO v_xattach IN BYTE MODE.
ENDIF.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = v_xattach
TABLES
binary_tab = i_contents_hex.
***set attachment name, count size*******************
DESCRIBE TABLE i_spfli LINES tab_lines.
tab_lines = tab_lines .
i_packing_list-transf_bin = 'X'.
i_packing_list-head_start = 1.
i_packing_list-head_num = 0.
i_packing_list-body_start = 1.
i_packing_list-body_num = tab_lines.
i_packing_list-doc_type = 'XLS'.
i_packing_list-obj_name = 'ATTACHMENT'.
i_packing_list-obj_descr = 'spfli'.
i_packing_list-doc_size = tab_lines * 255.
APPEND i_packing_list.
ENDFORM. " DEFINE_MAIL_ATTACHMENT
*&---------------------------------------------------------------------*
*& Form GET_RECEIVERS
*&---------------------------------------------------------------------*
FORM get_receivers .
* Add the recipients email address
CLEAR i_receivers.
REFRESH i_receivers.
* SELECT * FROM zszmail.
i_receivers-receiver = 'sheng.yan@hand-china.com'."Email Address
i_receivers-rec_type = 'U'. "Internet address
i_receivers-notif_read = 'X'.
i_receivers-com_type = 'INT'.
i_receivers-notif_del = 'X'.
i_receivers-notif_ndel = 'X'.
APPEND i_receivers.
* ENDSELECT.
ENDFORM. " GET_RECEIVERS
*&---------------------------------------------------------------------*
*& Form SEND_EMAIL_MESSAGE
*&---------------------------------------------------------------------*
FORM send_email_message.
DATA: w_subrc LIKE sy-subrc.
* Call the FM to post the message to SAPMAIL
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = w_doc_data "Attributes of new document
put_in_outbox = 'X' "Flag: Move document to outbox after send
commit_work = 'X' "An explicit COMMIT WORK is to be set
IMPORTING
sent_to_all = w_sent_all
TABLES
packing_list = i_packing_list "Information about structure of data tables
object_header = i_object_header
contents_hex = i_contents_hex
contents_txt = i_contents_txt "ASCII contents of object and attachments
receivers = i_receivers "Document recipients with send attributes
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
w_subrc = sy-subrc.
WAIT UP TO 1 SECONDS.
IF w_subrc EQ 0.
SUBMIT rsconn01 WITH mode = 'INT'
* WITH output = 'X'
AND RETURN.
ENDIF.
ENDFORM. " SEND_EMAIL_MESSAGE
SO_OBJECT_SEND发送
实例代码如下:
该代码通过上传本地的文件上传至附件(经过测试不能上传Word文档,Excel文档是可以的),调用函数,启用rsconn01进行发送
REPORT zys088.
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 = ''.
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 = '1208255965@qq.com'.
APPEND gt_receivers.
gt_receivers-recesc = 'U'.
gt_receivers-sndex = 'X'.
gt_receivers-recextnam = 'sheng.yan@hand-china.com'.
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.
"当调用发送邮件程序没有及时发送时,设置等待时间即可
DATA:l_num TYPE p DECIMALS 2 LENGTH 3 VALUE '0.2'.
WAIT UP TO l_num SECONDS.
******直接发送邮件的代码********
SUBMIT rsconn01 WITH mode ='INT'
* WITH output = 'X'
AND RETURN.
ENDFORM. " SEND
OO 发送
主要通过类方法的方式实现,实例代码如下:
类: CL_BCS 发送邮件主要用到的功能类, 包括创建发送请求, 添加发送内容,添加发送地
址, 到最终的发送指令发出.
类: CL_DOCUMENT_BCS, 用来放置发送的内容.
类: CX_BCS,这是个异常类, 用于捕捉发送邮件过程中出现的异常
REPORT zys090.
DATA: send_request TYPE REF TO cl_bcs,
document TYPE REF TO cl_document_bcs,
fail TYPE REF TO cx_bcs,
recipient TYPE REF TO if_recipient_bcs.
DATA: ls TYPE string,
mailto TYPE ad_smtpadr,
main_text TYPE bcsy_text,
title TYPE so_obj_des.
ls = '该邮件用于测试演示程序'.
APPEND ls TO main_text.
title = '邮件测试'.
mailto = ''.
TRY.
*第一步: 创建发送请求
send_request = cl_bcs=>create_persistent( ).
*第二步: 创建整理发送内容
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = main_text
i_subject = title ).
*第三步: 添加邮件内容到发送请求
send_request->set_document( document ).
*第四步: 邮件地址转换
recipient = cl_cam_address_bcs=>create_internet_address( mailto ).
*第五步: 添加邮件地址到发送请求
send_request->add_recipient( recipient ).
*第六步: 设置立即发送和发件箱查看
send_request->set_send_immediately( 'X' ).
*第七步: 正式发送并提交作业
send_request->send( i_with_error_screen = 'X' ).
COMMIT WORK AND WAIT.
CATCH cx_bcs INTO fail.
ENDTRY.
邮件效果如下:
问题总结
- 多个服务器配置使用
- 定时延迟使用,更改节点配置注意细节
- 设置延迟是,不能重复发送
- 针对特定邮件应该使用定时程序运行不能使用全局定时器。(暂时带实际解决)
- 多种附件发送,xls 以及xlsx的格式导入。sf的pdf 转otf 注意结束点
pdf 去掉最后一行// 符合就是可以合并pdf 了 - 邮件内容的html 格式书写
- 错误提示
- 跟踪
- 邮件组
- sost 包装给用户使用 权限管理,系统各种细节配置
- 其他待考虑