首页 > 程序开发 > 综合编程 > 其他综合 >

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

2013-02-25

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 ma...

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代码。
[plain]
DATA : wa_text TYPE soli.
wa_text = &#39;<p><font size="3" face="arial">Thank you </font></p><br/>&#39;.
APPEND wa_text TO fp_it_text.
Fill the subject line with the appropriatesubject line to be displayed on the mail.
填写合适的的主题行,会显示在邮件的主题行上。
[plain]
Data : lv_obj_des TYPE so_obj_des.
lv_obj_des = &lsquo;test mail&rsquo;.
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 &ndash; 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 &ndash; Confidential, F &ndash; functional, O &ndash;standard).
I_sensitivity表示保密的邮件,这是一个可选的参数。(P - 机密,F &ndash; 实用,O &ndash; 正常)。
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将文件附加给邮件
[plain]
ob_document->add_attachment(
i_attachment_type = &#39;HTM&#39;
i_attachment_subject = &#39;XXXX&rsquo;
i_att_content_hex = it_data ).
Since attachment type is HTML pass HTM to i_attachment_type.
由于附件类型是HTML,传递HTM到i_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.
它是用来传递一个对象发送请求
&sect; 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_string到i_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&rsquo;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. www.2cto.com
ob_bcs->set_send_immediately( EXPORTINGi_send_immediately = abap_true ).
Pass value &lsquo;X&rsquo; 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事务。
[plain]
wa_text TYPE soli.
wa_text = &#39;<imgsrc="NAME.BMP"align="left"/> &#39;.
APPEND wa_text TO fp_it_text.
Construct the MIME repository as follows. 构建如下的MIME库。
[plain]
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 = &#39;_OBJECT_ID&#39;.
wa_query-value = &#39;NAME&#39;. “name of the picture
APPEND wa_query TO it_query.
CALL FUNCTION &#39;WWW_GET_MIME_OBJECT&#39;
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 &#39;DP_CREATE_URL&#39;
EXPORTING
type = &#39;image&#39;
subtype = &#39;X-UNKNOWN&#39;
size = lv_pic_size
lifetime = &#39;T&#39;
TABLES
data = it_pick_data
CHANGING
url = lv_url
EXCEPTIONS
OTHERS = 1.
Now follow the third step to attach the document as we sawbefore.
现在,按照第三步附加的文件
[plain]
ob_document->add_attachment(
i_attachment_type = &#39;BMP&#39;
i_attachment_subject = &#39;NAME&rsquo;
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
请让我知道,如果你需要任何说明
相关文章
最新文章
热点推荐