我们有时候要通过写程序去发送邮件。 以下实例,请参考,如若有问题,请读者留言。

 

DATA: send_request TYPE REF TO cl_bcs,
      document TYPE REF TO cl_document_bcs,
      recipient TYPE REF TO if_recipient_bcs,
      subject TYPE so_obj_des,
      content TYPE soli_tab,
      wa_content TYPE soli,
      email_adress TYPE ad_smtpadr,
      flag TYPE os_boolean,
      attachment_subject TYPE so_obj_des,
      attachment_content TYPE soli_tab,
      attachment_type TYPE so_obj_tp.

INITIALIZATION.
  email_adress = 'GUOLIANG.ZHOU@SAP.COM'.
  subject = 'ABAP automatically send email test'.
  attachment_subject = 'Attachment1'.
  attachment_type = 'BIN'.

* Fill email content and attachment's content
  DO 20 TIMES.
    wa_content-line+0(2) = sy-index.
    IF sy-index <= 10.
      APPEND wa_content TO content.
      CLEAR wa_content.
    ELSE.
      APPEND wa_content TO attachment_content.
      CLEAR wa_content.
    ENDIF.
  ENDDO.



START-OF-SELECTION.
* Create request
  send_request = cl_bcs=>create_persistent( ).

* Create receiver list
  CALL METHOD cl_cam_address_bcs=>create_internet_address
    EXPORTING
      i_address_string = email_adress
    RECEIVING
      result           = recipient.

* Create content
  CALL METHOD cl_document_bcs=>create_document
    EXPORTING
      i_type    = 'RAW'
      i_subject = subject   " Title
      i_text    = content   " Text in Email
    RECEIVING
      result    = document. " Email object

* Add attachment
  CALL METHOD document->add_attachment
    EXPORTING
      i_attachment_type    = attachment_type
      i_attachment_subject = attachment_subject
      i_att_content_text   = attachment_content.


* Add receiver list into request
  CALL METHOD send_request->add_recipient
    EXPORTING
      i_recipient = recipient.

* Add content into request
  CALL METHOD send_request->set_document
    EXPORTING
      i_document = document.

* Begin send email
  CALL METHOD send_request->send
    RECEIVING
      result = flag.
  COMMIT WORK AND WAIT.

* Imeddiately send
  SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.

  IF flag = 'X'.
    WRITE: 'Sucessfully'.
  ELSE.
    WRITE: 'Failed'.
  ENDIF.