ABAP技术:发送内容带有图片的电子邮件

Send an email with image in thebody along with the text.

发送内容带有图片的电子邮件

 

We come across a requirment to have an image inthe body of an email. For example, we may need to have an image to be embedded besidesignature, for examplecompany logo.

我们可能遇到要将图片放在邮件正文中的需求,例如:我们可能需要一个带图的签名,例如放着公司LOGO。


Lets see how to achieve it stepby step. 让我们来看看如何一步一步实现

Before embedding an image,Let's have a brief look how to send an email usingCL_BCS with an example code snippet keeping the main emphasis on the image part. These steps will be useful while embedding the image.

潜入图片之前,让我用示例代码片段简单介绍下如何使用CL_BCS发送一封带图片的电子邮件。嵌入图片时,这些步骤都会用到。

 

First step is to create persistent. 第一步创建persistent

o   Use the method CREATE_PERSISTENT of the class CL_BCS tosend request object of the standard class CL_BCS.

o   Create  BCS  document
      ob_bcs = cl_bcs=>create_persistent( ).

Second step is to create document.第二步创建document·         

Use the method CREATE_DOCUMENT of the classCL_BCS to create the message body.

ob_document =cl_document_bcs=>create_document(i_type        = 'HTM'
                                              i_subject     = fp_subject
                                                i_text        =fp_it_text      ).

Fill the fp_it_text with the html codingwhich you want to get displayed in the message body like below.

填写fp_it_text你想要得到像下面显示在邮件正文中的HTML代码。

DATA : wa_text TYPE soli.
 wa_text = '<p><font size="3" face="arial">Thank you </font></p><br/>'.
 APPEND wa_text TO fp_it_text.

Fill the subject line with the appropriatesubject line to be displayed on the mail.

填写合适的的主题行,会显示在邮件的主题行上。

Data : lv_obj_des  TYPE so_obj_des.
lv_obj_des = ‘test mail’.

We can also fill other parameters like i_type which indicates the type of thedocument which we are going to create.

我们也可以加入其它参数像i_type(表示我们要创建的文件类型)

I_length specifies the length of the document inbytes and it is an optional parameter.

I_length指定的长度,以字节为单位的文件,它是一个可选的参数。

I_language indicates the language and it is anoptional parameter.

I_language表示语言,它是一个可选的参数。

I_importance indicates the priority of the mail andit is also an optional parameter. (1- high 5-medium, 9 – low and the default is5 which is medium).

i_importance表示的电子邮件的优先级,并且它也是一个可选参数。1 - 高,5中,9 - 低,默认值是5既是中)。

I_sensitivity indicates the confidentiality of the mail and it isalso an optional parameter. (P – Confidential, F – functional, O –standard).

I_sensitivity表示保密的邮件,这是一个可选的参数。P - 机密,F – 实用,O – 正常)。

I_hex specifies the content has to bedisplayed in binary format and it is also optional.

i_hex指定的内容用二进制格式,它也是可选的。

I_sender specifies the reference address.

i_sender指定的参考地址。

 

Third step is to add an attachment to themail. 第三步是将附件添加到邮件中。

Use the method ADD_ATTACHMENT of the classCL_BCS to attach a document to the mail.

使用类CL_BCS的方法ADD_ATTACHMENT将文件附加给邮件

ob_document->add_attachment(
           i_attachment_type = 'HTM'
           i_attachment_subject = 'XXXX’
           i_att_content_hex = it_data ).

Since attachment type is HTML pass HTM to i_attachment_type.

由于附件类型是HTML,传递HTMi_attachment_type

Pass the name which you want it to be on attachment(attachmentname)to i_attachment_subject.

你希望它在附件的名称传递给i_attachment_subject.

In i_att_content_hex pass the data using the table IT_DATAwhich is of table length 255 and it will have contents.

在这个 i_att_content_hex 传递内表IT_DATA

I_attachment_size is the size of the attachment and it isan optional parameter.

I_attachment_size的附件大小,它是一个可选的参数。

I_language specifies the language of theattachment and it is also an optional parameter.

I_language指定语言的附件,它也是一个可选的参数。

I_attachment_text is used ifthe created document is in text format and it is also an optional parameter.

I_attachment_text,如果创建的文件是文本格式,它也是一个可选的参数。

 Fourthstep is to set document. 第四步是设置文档。

Use the method SET_DOCUMENT of the classCL_BCS to set the document.

用类CL_BCS中的方法SET_DOCUMENT设置文档

It is used to pass an object to the sendrequest created using CREATE_PERSISTENT method of class CL_BCS.

它是用来传递一个对象发送请求

§ ob_bcs->set_document(  EXPORTING i_document =ob_document ) .

Fifth step is to add recipient.. 第五步填写收件人

Use the method ADD_RECIPENT of the classCL_BCS to add recipient address to the mail.

使用类CL_BCS中的方法ADD_RECIPENT 填加收件人电子邮件地址

lv_mailid = lv_emailaddr.

            ob_recipient_internet =cl_cam_address_bcs=>create_internet_address( i_address_string = lv_mailid ).
            ob_bcs->add_recipient(  EXPORTING i_recipient  =ob_recipient_internet ).

Parameter i_recipient has to be populated using the referencevariable which further refers to the interface IF_RECIPIENT_BCS. Now use theinstance method

来填充参数i_recipient有进一步的参考变量是指接口IF_RECIPIENT_BCS

现在使用实例方法

CREATE_INTERNET_ADDRESS of the classCL_CAM_ADDRESS_BCS and pass the email id to the i_address_string parameter.

Now pass the return parameterob_recipient_string to i_recipient parameter of method

ADD_RECIPIENT.

现在通过的的返回参数ob_recipient_stringi_recipient参数的方法ADD_RECIPIENT

I_copy parameter is used when we want to send a copy ofthe message and it is an optional parameter.

I_copy参数,我们要发送邮件的副本,它是一个可选的参数。

I_no_forward is set when we don’t want to allowforwarding and it is an optional parameter.

I_no_forward是否允许转发,它是一个可选的参数。

I_blind_copy is set when want to send using BCCoption and it is also an optional parameter.

I_blind_copy,设置要发送的使用BCC选项,它也是一个可选的参数。

I_express is set when we want to send an expressmail and it is also an optional parameter.

I_express发送的快件时设置的,它也是一个可选的参数。

If we want to send mail to multiple Email IDswe have to call the above steps in a loop. Keep all the email ids in a tableand pass each one into lv_mailid.

如果我们想将邮件发送到多个电子邮件ID,我们必须在一个循环中调用上面的步骤。在一个表中添加所有电子邮件ID和传递给每一个lv_mailid

Sixth step is to set send immediately. 第六步是立即设置发送。

Use the method SET_SEND_IMMEDIATELY of theclass CL_BCS to send the mail immediately irrespective of next send process.

ob_bcs->set_send_immediately( EXPORTINGi_send_immediately = abap_true ).

Pass value ‘X’ to the i_send_immediately parameter.

Seventh step is to send the email. 第七步是发送电子邮件。

Use the method SEND of the class CL_BCS tosend the mail.

lv_result = =  ob_bcs->send( ).

 

First step is to upload the picture of thedesired size and the desired format(BMP is suggested) into the SMW0 transaction.

第一步用事务代码SMWO上传图片,大小和格式(建议BMP)。

Screen of SMW0transaction is as below.

如下屏幕SMW0事务。

 

Click on binary data for webRFC applicationsand execute. You get to see the below screen.

点击webRFC应用程序的二进制数据和执行。你能看到下面的屏幕。

 

Now execute without giving any values.

现在执行,不给予任何值。

 

Click onnew button and upload your image. Assign a package and transport also.

点击新建按钮,上传您的图片。

It will appear in the list as above. 它会出现在上述列表

Sometimes, you may encounter an error"No MIME type assigned to object". If so, go to SMW0 transaction,find "settings" in the menu bar and under that find "maintainMIME types" option.

有时候,你可能会遇到一个错误,“没有MIME类型分配给对象”。如果是这样的话,去SMW0事务,发现在菜单栏中的“设置”下,找到“保持MIME类型”选项。

Check whether the extension with which youare uploading is there or not. Maintain there and you will be able to upload.

检查是否您要上载的扩展是有或没有。维护在那里再上传。

Now go to step 2 as mentioned in the sending a simplemail part, fill the fp_it_text table as below. Give the name with which thepicture is stored in SMW0 transaction.

现在进入第2步中提到的发送一个简单的邮件部分,填补了fp_it_text如下表。提供的名称与该图像被存储在SMW0事务。

wa_text TYPE soli.
wa_text = '<imgsrc="NAME.BMP"align="left"/> '.
APPEND wa_text TO fp_it_text.

Construct the MIME repository as follows. 构建如下的MIME库。

Data : it_pick_data TYPE TABLE OF w3mime,
         it_query TYPE TABLE OF w3query,
         wa_query TYPE w3query,
         it_html TYPE TABLE OF w3html,
         lv_return_code TYPE  w3param-ret_code,
         lv_content_type TYPE  w3param-cont_type,
         lv_content_length TYPE  w3param-cont_len,
         lv_url(255) TYPE c,
         lv_pic_size TYPE i,
         lv_text_img TYPE so_text255.
    REFRESH it_query.
     wa_query-name = '_OBJECT_ID'.
     wa_query-value = 'NAME'. “name of the picture
     APPEND wa_query TO it_query.

     CALL FUNCTION 'WWW_GET_MIME_OBJECT'
       TABLES
         query_string        = it_query
         html               = it_html
         mime               = it_pick_data
       CHANGING
         return_code         = lv_return_code
         content_type        = lv_content_type
         content_length      = lv_content_length
       EXCEPTIONS
         invalid_table       = 1
         parameter_not_found = 2
         OTHERS             = 3.
 
     IF sy-subrc = 0.
       lv_pic_size = lv_content_length.
     ENDIF.
 
     CLEAR lv_url.
 
     CALL FUNCTION 'DP_CREATE_URL'
       EXPORTING
         type     = 'image'
         subtype  = 'X-UNKNOWN'
         size     = lv_pic_size
         lifetime = 'T'
       TABLES
         data     = it_pick_data
       CHANGING
         url      = lv_url
       EXCEPTIONS
         OTHERS   = 1.
 

Now follow the  third step  to attach the document as we sawbefore.

现在,按照第三步附加的文件

ob_document->add_attachment(
           i_attachment_type = 'BMP'
           i_attachment_subject = 'NAME’ 
           i_att_content_hex = it_pick_data ).

Follow the rest of the steps for sending themail normally.

 剩下的步骤请按照正常发送邮件。

Please letme know if you have need any clarifications 

请让我知道,如果你需要任何说明

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ABAP是一种用于SAP软件开发的编程语言,可以用于发送电子邮件。要在ABAP发送电子邮件,可以按照以下步骤进行操作: 1. 设置邮件配置:首先,需要在ABAP系统中进行邮件配置。可以使用事务码SCOT来配置SMTP服务器和端口号,以及其他邮件服务器的相关信息。确保邮件服务器已正确配置。 2. 定义变量和对象:在ABAP程序中,需要定义一些变量和对象,用于存储和传递电子邮件相关的信息。例如,需要定义一个实例化的CL_BCS类对象来处理邮件发送功能。 3. 设置电子邮件的属性:在发送电子邮件之前,需要设置相关的属性,例如发件人、收件人、抄送、邮件主题、邮件正文等。通过设置这些属性,可以控制邮件的格式和内容。 4. 添加附件(可选):如果需要发送附件,可以通过添加附件来实现。可以使用函数模块SO_ATTACHMENT_INSERT来将附件添加到电子邮件中。 5. 发送邮件:当所有的邮件属性和附件设置完毕后,使用CL_BCS类中的SEND方法来发送电子邮件电子邮件将会根据设置的属性和内容发送到指定的收件人。 6. 检查发送结果:发送邮件后,可以检查邮件发送结果。可以使用CL_BCS类中的GET_SENT_MESSAGES方法来获取电子邮件发送的结果信息。 以上是使用ABAP发送电子邮件的基本步骤。在实际的开发中,可能还有其他的细节需要注意,例如设置邮件服务器的安全性、处理邮件发送失败的情况等。但总体来说,通过合适的配置和正确的代码编写,可以在ABAP中成功发送电子邮件。 ### 回答2: ABAP是一种针对SAP应用程序开发的编程语言,可以与SAP系统进行交互。在ABAP中,可以使用几种方法来发送电子邮件。 首先,可以使用SAP提供的Function Module(函数模块)来发送电子邮件。例如,使用函数模块SO_NEW_DOCUMENT_ATT_SEND_API1可以发送电子邮件,并且可以附加文件。这个函数模块可以设置电子邮件发送者、接收者、主题、正文和附件等信息。 其次,可以使用ABAP邮件发送类CL_BCS来发送电子邮件。CL_BCS是SAP提供的一个类,可以方便地发送电子邮件。你可以通过实例化CL_BCS类,并设置电子邮件的相关属性,例如发送者、接收者、主题、正文和附件等。然后,可以调用该类的SEND方法来发送邮件。 第三种方法是使用SMTP服务器发送电子邮件。在ABAP中,可以使用函数模块INTERNET_SMTP_MAIL发送电子邮件。通过设置相关参数,例如SMTP服务器地址、发送者、接收者、主题和正文等,可以使用该函数模块发送邮件。 无论使用哪种方法,发送电子邮件之前,需要确保SAP系统已经配置了正确的邮件服务器参数,并且相应的授权已经设置。此外,还需要在后台任务管理器中设置最小运行时间,以便邮件可以发送成功。 综上所述,使用ABAP发送电子邮件有多种方法可供选择,根据具体情况选择合适的方法,并提供必要的参数,即可成功发送电子邮件。 ### 回答3: ABAP 是一种专门用于 SAP 系统的编程语言,可以在 SAP 系统内部使用 ABAP 编写各种程序和功能。发送电子邮件是其中的一项常见需求,下面我将使用 ABAP 来演示如何发送电子邮件。 首先,我们需要配置一个可用的 SAP 发件人邮件账户。在 SAP 系统中,可以通过访问交易码 SCOT 进行相关配置。在 SCOT 页面中,我们可以设置发送邮件所需的 SMTP 服务器和认证信息。 接下来,在 ABAP 程序中编写代码来发送邮件。我们可以使用函数模块 `SO_NEW_DOCUMENT_ATT_SEND_API1` 来发送带有附件的邮件。以下是一个示例代码: ```abap DATA: lv_sender TYPE sy-uname VALUE 'your_sender', lv_recipient TYPE ad_smtpadr, lt_attachment TYPE STANDARD TABLE OF solisti1, lw_attachment TYPE solisti1, lv_subject TYPE so_obj_des, lv_text TYPE soli_tab, lt_receivers TYPE STANDARD TABLE OF somlreci1, lw_receiver TYPE somlreci1. * 设置发件人和收件人 lv_recipient = 'your_recipient'. lw_receiver-receiver = lv_recipient. APPEND lw_receiver TO lt_receivers. * 设置邮件主题 lv_subject = '邮件主题'. * 设置邮件正文 lv_text = '邮件正文'. * 添加附件(可选) CLEAR lw_attachment. lw_attachment-obj_descr = '附件描述'. lw_attachment-obj_name = 'file_name.pdf'. "附件文件名 lw_attachment-obj_lang = 'ZH'. "附件语言 lw_attachment-obj_len = strlen( '附件内容' ). "附件内容长度 lw_attachment-objbin = '附件内容'. APPEND lw_attachment TO lt_attachment. * 发送邮件 CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING document_data = lw_send_request TABLES packing_list = lt_attachment object_header = lt_receivers contents_txt = lv_text contents_bin = lt_attachment 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. ``` 在上述示例中,我们定义了发件人、收件人、邮件主题、邮件正文和附件信息,并将其传递给函数模块 `SO_NEW_DOCUMENT_ATT_SEND_API1` 进行发送。请注意,发送邮件操作可能需要相应的权限和配置,具体情况需根据实际系统进行调整。 以上就是使用 ABAP 发送电子邮件的基本步骤和示例代码。通过以上的操作,我们可以在 SAP 系统中使用 ABAP发送电子邮件,并且可以添加附件等特性。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值