邮件相关

 

邮件相关一:SO00

SCOT的邮箱配置一般有basis处理

tcode:

SO00 :S O 零零

 

点击执行后,还并没有发送过去

进入scot

 

 

 

邮件相关二:DEMO 发邮件

FUNCTION ZLM_TEST_MAIL_01.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(MAIL_TITLE) TYPE  SO_OBJ_DES OPTIONAL
*"     REFERENCE(MAIL_TEXT) TYPE  STRING
*"  EXPORTING
*"     VALUE(ERROR_TEXT) TYPE  STRING
*"  TABLES
*"      SEND_LIST STRUCTURE  SOLISTI1 OPTIONAL
*"----------------------------------------------------------------------

  DATA: BEGIN OF GT_SEND OCCURS 0.             "E-mail address
          INCLUDE STRUCTURE SOMLRECI1.
  DATA: END OF GT_SEND.

  DATA: GS_DATA LIKE SODOCCHGI1,              "E-mail setting
        TAB_LINES  TYPE I,
        DOC_SIZE   TYPE I.
  DATA: L_TEXT(50).

  DATA: BEGIN OF GT_ITAB OCCURS 0,            "E-mail text
          TEXT(255),
        END OF GT_ITAB.

  DATA: IT_SITAB TYPE TABLE OF STRING.
  SPLIT MAIL_TEXT AT '/n/t' INTO TABLE GT_ITAB.

* Email的接收人员
  LOOP AT SEND_LIST.
    GT_SEND-RECEIVER     = SEND_LIST-LINE.
    GT_SEND-REC_TYPE     =  'U'.
    GT_SEND-EXPRESS      =  'X'.
    APPEND GT_SEND.
  ENDLOOP.

  CLEAR: GS_DATA.

  GS_DATA-OBJ_NAME     =  'URGENT'.
  GS_DATA-OBJ_DESCR    =  MAIL_TITLE. "邮件标题
  GS_DATA-OBJ_LANGU    =  '1'. "中文
*GS_DATA-SENSITIVTY   =  'P'.
  GS_DATA-OBJ_PRIO     =  '1'.
  GS_DATA-NO_CHANGE    =  'X'.
  GS_DATA-PRIORITY     =  '1'.
*   Write Packing List (Main)
  DESCRIBE TABLE GT_ITAB LINES TAB_LINES.
  READ     TABLE GT_ITAB INDEX TAB_LINES.
  GS_DATA-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( GT_ITAB ).

* SEND THE EMAIL
  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
    EXPORTING
      DOCUMENT_DATA              = GS_DATA
      DOCUMENT_TYPE              = 'RAW'
      PUT_IN_OUTBOX              = 'X'
      COMMIT_WORK                = 'X'
    TABLES
      OBJECT_CONTENT             = GT_ITAB
      RECEIVERS                  = GT_SEND
    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.

  IF SY-SUBRC NE 0.
    ERROR_TEXT = '邮件发送失败'.
  ElseIF SY-SUBRC = 0.
* 
* 如果不自动送,加上执行邮件立即发送程序代码
  SUBMIT RSCONN01 WITH MODE = 'INT'
                WITH OUTPUT = 'X' "显示发送结果  可以使用空 
                AND RETURN.
  MESSAGE '发送成功' TYPE 'S'.
  ENDIF.

ENDFUNCTION.

邮件相关三:Demo 发邮件的Report(带附件)

* Data Declaration
DATA: docdata    LIKE sodocchgi1,
      objpack    LIKE sopcklsti1 OCCURS  1 WITH HEADER LINE,
      objhead    LIKE solisti1   OCCURS  1 WITH HEADER LINE,
      objtxt     LIKE solisti1   OCCURS 10 WITH HEADER LINE,
      objbin     LIKE solisti1   OCCURS 10 WITH HEADER LINE,
      objhex     LIKE solix      OCCURS 10 WITH HEADER LINE,
      reclist    LIKE somlreci1  OCCURS  1 WITH HEADER LINE.
DATA: BEGIN OF it_obj_descr  OCCURS 0,
        obj_desc LIKE objpack-obj_descr ,
      END OF it_obj_descr.
DATA: listobject LIKE abaplist   OCCURS  1 WITH HEADER LINE.

DATA: tab_lines  TYPE i,
      des_lines  TYPE i,
      doc_size   TYPE i,
      att_type   LIKE soodk-objtp.

PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY,
            p_email TYPE so_recname OBLIGATORY.                        "收件人邮箱

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  DATA: tmp_filename LIKE rlgrap-filename.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
*     def_filename     = p_file
      mask             = ',*.XLSX,*.XLSX;,*.TXT,*.TXT;,*.PDF,*.PDF;,*.*,*.*.'
      mode             = 'O'
    IMPORTING
      filename         = tmp_filename
    EXCEPTIONS
      inv_winsys       = 01
      no_batch         = 02
      selection_cancel = 03
      selection_error  = 04.
  IF sy-subrc = 0.
    p_file = tmp_filename.
  ENDIF.

START-OF-SELECTION.
  DATA: l_file TYPE string.
  l_file = p_file.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = l_file
      filetype                = 'BIN'                  "获取时采用二进制
*     HAS_FIELD_SEPARATOR     = 'X'
    TABLES
      data_tab                = objbin[]
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* NOTE: 以下部分为制作邮件的标题,文本内容等信息

* Create Message Body
*   Title and Description

  docdata-obj_name  = 'ADAF'.                 "文档名称 名称随便取
  docdata-obj_descr = '测试附件传输'.         "邮件的主题

  "邮件正文
*   Main Text
  objtxt = 'Test Document.'.
  APPEND objtxt.
  objtxt = 'HAVE A NICE DAY.'.
  APPEND objtxt.

*   Write Packing List (Main)
  DESCRIBE TABLE objtxt LINES tab_lines.
  READ     TABLE objtxt INDEX tab_lines.
  docdata-doc_size = ( tab_lines - 1 ) * 255 + strlen( objtxt ).
  CLEAR objpack-transf_bin.
  objpack-head_start = 1.
  objpack-head_num   = 0.
  objpack-body_start = 1.
  objpack-body_num   = tab_lines.
  objpack-doc_type   = 'RAW'.
  APPEND objpack.

* 获取附件文件名称
  SPLIT l_file AT '\' INTO TABLE it_obj_descr.
  DESCRIBE TABLE it_obj_descr LINES des_lines.
  READ     TABLE it_obj_descr INDEX des_lines.

* 这里开始添加附件相关信息
  att_type = 'EXT'. "这里可能的输入参数可以参考表TSOTD, EXT表示外部文件

  DESCRIBE TABLE objbin LINES tab_lines.
  READ     TABLE objbin INDEX tab_lines.
  objpack-doc_size = ( tab_lines - 1 ) * 255 + strlen( objbin ).                          "附件的大小
  objpack-transf_bin = 'X'.       "表示以二进制传输,由于上传时选择二进制,所以这里必须给值
  objpack-head_start = 1.
  objpack-head_num   = 0.
  objpack-body_start = 1.         "附件内容开始行,如果添加多个附件则下一个附件的起始行为 TAB_LINES + 1
  objpack-body_num   = tab_lines. "附件内容总行数
  objpack-doc_type   = att_type.
  objpack-obj_name   = 'ATTACHMENT'.
  objpack-obj_descr  = it_obj_descr-obj_desc.                 "附件的名称
  APPEND objpack.

  "收件人信息----邮箱
* CREATE RECEIVER LIST
  reclist-receiver = p_email.  "接收人邮箱
  reclist-rec_type = 'U'.
  reclist-express      =  'X'.
*RECLIST-REC_TYPE = 'A'.
  APPEND reclist.


"调用函数发送邮件
* SEND MESSAGE
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data                    = docdata "邮件标题的属性
      put_in_outbox                    = 'X'
      commit_work                      = 'X'     "USED FROM REL. 6.10
*   IMPORTING
*     SENT_TO_ALL                      =
*     NEW_OBJECT_ID                    =
    TABLES
      packing_list                     = objpack   "传输内容格式
      object_header                    = objhead
      contents_bin                     = objbin    "附件内容
      contents_txt                     = objtxt    "邮件文字内容
*     CONTENTS_HEX                     = OBJHEX
*     OBJECT_PARA                      =
*     OBJECT_PARB                      =
      receivers                        = reclist   "接收人列表
*   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
          .
 
************************************************
* 如果不自动发送,就加上执行邮件立即发送程序
    SUBMIT rsconn01 WITH mode = 'INT'
                  WITH output = 'X' "显示发送结果 ,
                  AND RETURN.

邮件相关四:Sf转PDF 发邮件

 

 

REPORT ZWB02.
*"----------------------------------------------------------------------
*发邮件
  DATA: lv_fm_name TYPE rs38l_fnam.
  DATA: ls_job_out_info         TYPE ssfcrescl,
        ls_document_output_info TYPE ssfcrespd,
        ls_job_out_options      TYPE ssfcresop.
  DATA: lt_otfdata      TYPE ssfcrescl-otfdata,
        lt_lines        TYPE TABLE OF tline,
        lv_bin_filesize TYPE i,
        lv_bin_file     TYPE xstring.
  DATA: lv_length  TYPE so_obj_len,
        lv_subject TYPE so_obj_des,
        lt_text    TYPE soli_tab.
  DATA: lt_item     TYPE TABLE OF zsd_f001_s WITH HEADER LINE,
        ls_item     TYPE  zsd_f001_s,
        lt_item_col TYPE TABLE OF zsd_f001_s,
        ls_item_col TYPE zmms002.
  DATA: lv_num   TYPE int4.
  DATA: ls_lfa1 TYPE lfa1.
  DATA: lv_werks TYPE lfa1-lifnr.
  DATA: lt_log TYPE TABLE OF zmmt002_log,
        ls_log TYPE zmmt002_log,
        lv_msg TYPE string.
*call
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZSDSF01_MD'
    IMPORTING
      fm_name            = lv_fm_name
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    CLEAR: lv_msg.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO lv_msg.
    ls_log-zmsg = lv_msg && ';' && ls_log-zmsg.
  ENDIF.

  DATA(ls_control_parameters) = VALUE ssfctrlop( no_dialog = 'X' preview = '' getotf = 'X' langu = sy-langu ).
  DATA(ls_output_options) = VALUE ssfcompop( tddest = 'LP01' tdprinter = 'CNSAPWIN' ).

*process
  lt_item[] = ot_table[].

  CALL FUNCTION lv_fm_name
    EXPORTING
      control_parameters   = ls_control_parameters
      output_options       = ls_output_options
    IMPORTING
      document_output_info = ls_document_output_info
      job_output_info      = ls_job_out_info
      job_output_options   = ls_job_out_options
    TABLES
      i_table              = ot_table
    EXCEPTIONS
      formatting_error     = 1
      internal_error       = 2
      send_error           = 3
      user_canceled        = 4
      OTHERS               = 5.
  IF sy-subrc NE 0.
    CLEAR: lv_msg.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO lv_msg.
    ls_log-zmsg = lv_msg && ';' && ls_log-zmsg.
  ENDIF.

  lt_otfdata[] = ls_job_out_info-otfdata[].

  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
*     max_linewidth         = 132
*     ARCHIVE_INDEX         = ' '
*     COPYNUMBER            = 0
*     ASCII_BIDI_VIS2LOG    = ' '
*     PDF_DELETE_OTFTAB     = ' '
*     PDF_USERNAME          = ' '
*     PDF_PREVIEW           = ' '
*     USE_CASCADING         = ' '
*     MODIFIED_PARAM_TABLE  =
    IMPORTING
      bin_filesize          = lv_bin_filesize
      bin_file              = lv_bin_file
    TABLES
      otf                   = lt_otfdata[]
      lines                 = lt_lines
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      err_bad_otf           = 4
      OTHERS                = 5.
  IF sy-subrc NE 0.
    CLEAR: lv_msg.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO lv_msg.
    ls_log-zmsg = lv_msg && ';' && ls_log-zmsg.
  ENDIF.

  DATA:l_bu_sort1 TYPE but000-bu_sort1.
  CLEAR:l_bu_sort1.
  SELECT SINGLE bu_sort1 INTO l_bu_sort1 FROM but000 WHERE partner = iv_kunnr.
  TRY.
      DATA(send_request) = cl_bcs=>create_persistent( ).
      DATA(sender) = cl_cam_address_bcs=>create_internet_address( i_address_string = 'purchaseOrder@sxmsh.com'
                                                                  i_address_name = |{ l_bu_sort1 }| ).

      DATA(lo_send_request) = cl_bcs=>create_persistent( ).
      DATA(lo_document_bcs) = NEW cl_document_bcs( ).
*设置内容
      lt_text = VALUE #( BASE lt_text ( line = '请查收附件订单,谢谢!' ) ).
      lt_text = VALUE #( BASE lt_text ( line = '如有疑问请联系订货人员!' ) ).
      lv_subject =  |门店订货系统单号:{ iv_order } |.
      lo_document_bcs = cl_document_bcs=>create_document( i_type    = 'RAW'
                                                          i_length  = lv_length
                                                          i_text    = lt_text
                                                          i_subject = lv_subject
                                                          i_language   = sy-langu
                                                          i_importance = '1'
                                                          ).
*添加附件
*      lv_subject = 'PO' && ot_table-znum && `_` && sy-datum && sy-uzeit.
      lv_subject = |PO_{ iv_order }_{ sy-datum }|.
      lo_document_bcs->add_attachment( EXPORTING i_attachment_type    = 'PDF'
                                                 i_att_content_hex    = cl_document_bcs=>xstring_to_solix( lv_bin_file )"pdf content
                                                 i_attachment_subject = lv_subject
                                                 ).
*设置邮箱并发送
*---接收人
      DATA(ls_lifnr) = VALUE #( gt_show[ 1 ] OPTIONAL ).
      SELECT SINGLE *         INTO @DATA(ls_lfa1_tmp) FROM lfa1 WHERE lifnr      EQ @ls_lifnr-lifnr.
      SELECT SINGLE smtp_addr INTO @lv_address  FROM adr6 WHERE addrnumber EQ @ls_lfa1_tmp-adrnr."邮箱

*      lv_address = '26747405@QQ.COM'  .
      lv_address1 = 'TYCAICDGOU@GTMSH.COM'  .
 
*---发件人
      CALL METHOD lo_send_request->set_sender
        EXPORTING
          i_sender = sender.


      lo_send_request->set_document( lo_document_bcs ).
      lo_send_request->add_recipient( cl_cam_address_bcs=>create_internet_address( lv_address ) )."recipient
      lo_send_request->add_recipient( i_recipient = cl_cam_address_bcs=>create_internet_address( lv_address1 )
                                       i_copy     = 'X' )."copy recipient
      lo_send_request->set_send_immediately( 'X' ).  "no hesitation

*返回消息
      DATA(lv_result) = lo_send_request->send( i_with_error_screen = 'X' ).
      IF lv_result IS INITIAL."error
        CLEAR: lv_msg.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO lv_msg.
        ls_log-zmsg = lv_msg && ';' && ls_log-zmsg.
      ENDIF.

    CATCH cx_bcs INTO DATA(lo_bcs_exception).
      lv_msg = lo_bcs_exception->get_text(  ).
  ENDTRY.
*cococo*
  COMMIT WORK AND WAIT.
*cococo*

*save log
  APPEND VALUE #(
  ebeln       = iv_ebeln
  zrecv_email = lv_address
  ztype       = COND #( WHEN lv_result IS NOT INITIAL THEN 'S' ELSE 'E' )
  zmsg        = COND #( WHEN lv_result IS NOT INITIAL THEN '邮件发送成功(订货上传)' ELSE '邮件发送失败邮件发送成功(订货上传)' && ls_log-zmsg )
  zdate       = sy-datum
  zuser       = sy-uname
  ztime       = sy-uzeit ) TO lt_log.
  MODIFY zmmt002_log FROM TABLE lt_log.

邮件相关五:上传excel附件并发送Email(加强版)

REPORT ZXP1.
PARAMETERS p_file LIKE rlgrap-filename OBLIGATORY.
*&---------------------------------------------------------------------*
*&   Event AT SELECTION-SCREEN ON
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM frm_file_open_f4 CHANGING p_file.
*&---------------------------------------------------------------------*
*&   Event AT START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM frm_send_mail.

*&---------------------------------------------------------------------*
*&      Form  FRM_FILE_OPEN_F4
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_P_FILE  text
*----------------------------------------------------------------------*
FORM frm_file_open_f4 CHANGING file.

  DATA: lit_file      TYPE filetable,
        l_file_filter TYPE string,
        l_rc          TYPE i.
  FIELD-SYMBOLS: <fs_file> LIKE LINE OF lit_file.

  l_file_filter = 'Excel Template (*.xlt)|*.xlt|Excel Workbook (*.xls)|*.xls|All files (*.*)|*.*'.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = 'Choose template file ...'
      default_extension       = '*.*'
      "file_filter             = l_file_filter
    CHANGING
      file_table              = lit_file
      rc                      = l_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.

  IF sy-subrc = 0.
    READ TABLE lit_file ASSIGNING <fs_file> INDEX 1.

    IF sy-subrc = 0.
      file = <fs_file>-filename.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_SEND_MAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_send_mail .

  DATA:lo_document      TYPE REF TO cl_document_bcs, "用来放发送的内容的类
       lit_contents     TYPE soli_tab,
       l_cc             TYPE adr6-smtp_addr,
       l_to             TYPE adr6-smtp_addr,
       l_bcs_to         TYPE REF TO if_recipient_bcs,
       l_bcs_cc         TYPE REF TO if_recipient_bcs,
       lo_sender        TYPE REF TO cl_sapuser_bcs,
       l_result         TYPE os_boolean,
*      cl_bcs发送邮件主要用到的功能类, 包括创建发送请求, 添加发送内容,添加发送地址, 到最终的发送指令发出.
       w_document       TYPE REF TO cl_bcs,
       l_file_size_char TYPE so_obj_len,
       l_filen          TYPE string,
       lit_mailhex      TYPE solix_tab,
       l_file_size      TYPE i,
       l_rc             TYPE i,
       l_string         TYPE string,
       l_subject        TYPE so_obj_des,
*      cx_bcs异常类, 捕捉发送邮件过程中出现的异常.
       lo_fail          TYPE REF TO cx_bcs.


* ATTACH THE FILE, THE ATTACHMENT TYPE SHOULD BE BIN TO ACCEPT ANY KIND OF ATTACHMENT, INCLUDING VIDEOS, AUDIO FILES ETC...

  l_filen = p_file.
* 读取附件
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = l_filen
      filetype                = 'BIN'  "
*     has_field_separator     = SPACE
*     header_length           = 0
*     read_by_line            = ''
*     dat_mode                = SPACE
*     codepage                = SPACE
*     ignore_cerr             = ABAP_TRUE
*     replacement             = '#'
*     virus_scan_profile      =
    IMPORTING
      filelength              = l_file_size
*     header                  =
    CHANGING
      data_tab                = lit_mailhex
*     isscanperformed         = SPACE
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      not_supported_by_gui    = 17
      error_no_gui            = 18
      OTHERS                  = 19.
  IF sy-subrc <> 0.
* Implement suitable error handling here
    RETURN.
  ENDIF.
* 附件长度,这个很重要,一定要有
  l_file_size_char = l_file_size.
* 邮件正文
  l_string = '哈喽,地球人,点开附件有惊喜'.
  APPEND l_string  TO lit_contents.

  TRY.
*     CREATE THE DOCUMENT WITH CONTENTS
      CREATE OBJECT lo_document.
      lo_document = cl_document_bcs=>create_document(
          i_type       = 'HTM'
          i_subject    = '一封来自火星的测试邮件'  "邮件标题
          i_length     = l_file_size_char
          i_language   = sy-langu
          i_importance = '1'
          i_text       = lit_contents
          ).

*     hard code 附件为excel
      l_subject = '附件.xls'.
*     添加附件,可以添加多个附件
      CALL METHOD lo_document->add_attachment
        EXPORTING
          i_attachment_type    = 'BIN'
          i_attachment_subject = l_subject
          i_attachment_size    = l_file_size_char
          i_att_content_hex    = lit_mailhex.

*     CREATING PERSISTENT OBJECT WILL ALLOW YOU TO SET THE DOCUMENT IN THE MAIL
      w_document = cl_bcs=>create_persistent( ).

*     发件人
*     前提是这个邮箱地址能发邮件,并且不需要密码
      lo_sender = cl_sapuser_bcs=>create( sy-uname ).
      w_document->set_sender( lo_sender ).

*     收件人
      l_to = 'mars@mars.com'.
      l_bcs_to = cl_cam_address_bcs=>create_internet_address( l_to ).
*     Add recipient to send request
      CALL METHOD w_document->add_recipient
        EXPORTING
          i_recipient = l_bcs_to.

*    抄送人
      l_cc = 'test@test.com'.
      l_bcs_cc = cl_cam_address_bcs=>create_internet_address( l_cc ).

      CALL METHOD w_document->add_recipient
        EXPORTING
          i_recipient = l_bcs_cc
          i_copy      = 'X'.
      " i_express   = 'X'.

*     SEND THE MAIL
      w_document->set_send_immediately( 'X' ).  "设置立即发送
      w_document->send_request->set_link_to_outbox( 'X' ). "与outbox关联
      CALL METHOD w_document->set_document( lo_document ).
      CALL METHOD w_document->send(
        EXPORTING
          i_with_error_screen = 'X'
        RECEIVING
          result              = l_result ).
    CATCH cx_bcs INTO lo_fail.
  ENDTRY.
* YOU CAN VERIFY THE STATUS IN THE LIST, YOU CAN ALSO SUBMIT THIS AS A BACKGROUND JOB.

  IF l_result = 'X'.
    COMMIT WORK AND WAIT.
    MESSAGE '发送成功' TYPE 'S'.
  ELSE.
    ROLLBACK WORK.
    MESSAGE '发送失败' TYPE 'S' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.

ENDFORM.

 邮件相关六:邮件带图片

邮件页面添加图片

添加图片前先将图片文件上传值MIME资料库

上传方法:参考网页https://blogs.sap.com/2013/06/11/image-in-mail-body-in-abap/

http://saptechnical.com/Tutorials/ABAP/email/Index.htm

将图像上传到MIME存储库中。

  •      Mime存储库中有许多选项可以上传图像。我已经使用SE80 TCODE 实现了。
  •       也可以通过使用
    • 类别: CL_MIME_REPOSITORY_API
    • 方法: IF_MR_APIPUT

      1.提供TCODE SE80

      2.单击Mime存储库

     3.选择要上传图像的目录,右键单击目录,然后选择“ 导入MIME对象 ”(此处的目录为“ PUBLIC”)。

     

     4.浏览系统并选择要从系统上载的所需图像,然后将出现以下屏幕,提供说明并单击保存按钮。

     5.在保存映像时,系统将提示您输入对象包,然后根据需要将其保存在关联的包或本地对象中。

     6.现在,图像将出现在文件夹中,并且通过单击图像,我们可以得到如下所示的预览。

 

2. xstring的形式从mime 存储库中获取图像

  • 根据所需的逻辑创建报告(要求发送生日邮件),我们可以在其中添加图片部分和html正文
  • 使用以下命令获取存储在报告中的mime存储库中的图像 
    • 类:cl _mime_repository_api 
    • 方法:if_ mr _apiget_api
  • 在参数i _ url中,将给出图像路径以获得xstring形式的图像。
  • 码:

3.从转换图像xstring到表格形式(我 .ESolix _TAB

  • 在变量gv _content,图像采用xstring的形式。
  • 要在邮件中附加该图像,我们必须将其转换为Xstring的表格形式,其中包含类型为xstring且长度为255 char的行
  • LT _ Solix表包含在图像xstring这将是表的形式附加到邮件主体。

4.将图片附加到xstring

4. Attach Image in xstring

  •   In variable l_filename the name is given to the image that is created and in variable      l_content_id the content id (CID) is assigned which   would be later used in the html content.
  • The variable lt_solix contains the image in xstring table form.
  • The variable l_obj_len contains the length of image which was calculated earlier
  • The content type is the mime type of the content in lt_solix
  • For .jpg image it is image/jpg. Similarly for bmp – image/bmp, gif – image/gif , etc
  •   Attach image using 
    • Class : cl_gbt_multirelated_service
    • Method: add_binary_part
    • Object : lo_mime_helper
  • CODE:

     

页面图片示例代码仅限于内容部分、

 """获取邮件图片
      DATA: o_mr_api         TYPE REF TO if_mr_api.
      DATA is_folder TYPE boole_d.
      DATA l_current TYPE xstring.
      DATA l_loio TYPE skwf_io.
      IF o_mr_api IS INITIAL.
        o_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ).
      ENDIF.
      CALL METHOD o_mr_api->get
        EXPORTING
          i_url              = '/SAP/PUBLIC/TWS.BMP'
        IMPORTING
          e_is_folder        = is_folder
          e_content          = l_current
          e_loio             = l_loio
        EXCEPTIONS
          parameter_missing  = 1
          error_occured      = 2
          not_found          = 3
          permission_failure = 4
          OTHERS             = 5.
      DATA:b64data TYPE string.
      CALL FUNCTION 'SSFC_BASE64_ENCODE'
        EXPORTING
          bindata = l_current
        IMPORTING
          b64data = b64data.
      IF sy-subrc <> 0.

      ENDIF.
      DATA:wa_mail_body TYPE string.
      CLEAR wa_mail_body.
*add image base64 content
*      wa_mail_body = '<img src="data:TWS/BMP;base64,'.

      APPEND  '<img src="data:TWS/BMP;base64,' TO main_text.

      DATA: lv_length TYPE i,
             lv_len2 TYPE i.

      lv_length = STRLEN( b64data ).
      lv_len2 = lv_length / 255.

*      wa_mail_body = b64data.
      APPEND  b64data TO main_text.
*      APPEND wa_mail_body TO main_text.
      CLEAR wa_mail_body.
      DATA lv_len3 TYPE i.
      DATA: temp1 TYPE i,
            temp2 TYPE i.
      DO lv_len2 TIMES.
        lv_len3 = 255 * sy-index.

        IF lv_len3 <= lv_length.
          wa_mail_body = b64data+lv_len3.
          IF wa_mail_body IS NOT INITIAL.
            APPEND wa_mail_body TO main_text.
            CLEAR wa_mail_body.
          ELSE.
            EXIT.
          ENDIF.
        ELSEIF lv_len3 > lv_length.

          EXIT.

        ENDIF.
      ENDDO.


      REFRESH:objtxt.
      APPEND '"alt="Happy birthday" align="middle" width="304" height="228" />'TO main_text.
邮件相关五:上传excel附件并发送Email(加强版)
posted @ 2019-09-24 18:51  蜡哲图  阅读(473)  评论(0编辑  收藏  举报
目录内容