<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Regarding Conversion of Binary to Base64 for Mail attachemnt in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Regarding-Conversion-of-Binary-to-Base64-for-Mail-attachemnt/m-p/981365#M379092</link>
    <description>&lt;DIV class="x_elementToProof" data-olk-copy-source="MessageBody"&gt;We have successfully migrated our SAS email functionality from SMTP with Basic Authentication to an OAuth2-based approach using Microsoft Azure Portal and Microsoft Graph API.&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;Currently, we are able to trigger emails successfully using OAuth2. However, we are facing an issue while sending&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Excel file attachments&lt;/STRONG&gt;.&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;&lt;SPAN&gt;As part of the OAuth2-based email implementation, we are converting the Excel (.xlsx) file into Base64 format and attaching it using the&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;content Bytes&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;attribute in the Graph API request. Although the email is delivered successfully with the attachment, the Excel file fails to open when downloaded from the email and shows an error indicating that the file is corrupted or the format is invalid.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SatishR_0-1767096495653.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/112454i5AC502C43EF1FC36/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SatishR_0-1767096495653.png" alt="SatishR_0-1767096495653.png" /&gt;&lt;/span&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;We have attached the following for your reference:&lt;/DIV&gt;
&lt;UL data-spread="false"&gt;
&lt;LI&gt;
&lt;DIV class="x_elementToProof" role="presentation"&gt;SAS code used for converting the Excel file into Base64&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class="x_elementToProof" role="presentation"&gt;SAS log generated during the Base64 conversion and email trigger process&lt;BR /&gt;&lt;BR /&gt;
&lt;P&gt;/* 1. Set options for long strings */&lt;BR /&gt;options noquotelenmax;&lt;BR /&gt;%let report_date = %sysfunc(intnx(day,%sysfunc(today()),-1),date11.);&lt;/P&gt;
&lt;P&gt;/* 2. Define filenames */&lt;BR /&gt;filename excel_in "/home/sas/DO Efficiency Report.xlsx" recfm=n;&lt;BR /&gt;filename json_req temp lrecl=32767;&lt;/P&gt;
&lt;P&gt;/* 3. Convert Excel to Base64 and build JSON */&lt;BR /&gt;data _null_;&lt;BR /&gt;file json_req;&lt;BR /&gt;&lt;BR /&gt;infile excel_in;&lt;BR /&gt;length file_content $10547; &lt;BR /&gt;input file_content $char10547.;&lt;BR /&gt;&lt;BR /&gt;length b64_val $14064; &lt;BR /&gt;b64_val = put(file_content, $base64x14064.);&lt;/P&gt;
&lt;P&gt;/* Build the JSON payload with your specific draft */&lt;BR /&gt;put '{'&lt;BR /&gt;/ ' "message": {'&lt;BR /&gt;/ ' "subject": "Daily Reports of &amp;amp;report_date",'&lt;BR /&gt;/ ' "body": {'&lt;BR /&gt;/ ' "contentType": "Text",'&lt;BR /&gt;/ ' "content": "Dear Sir,\n\nPlease find below daily reports of &amp;amp;report_date for your reference,\n\n1. Call Summary Report (Rural &amp;amp; Urban)\n2. Call Summary Report\n3. Do Efficiency Report\n4. ERV Efficiency Report\n5. ERV Tracking Report\n6. Women Related Call summary\n\nThanks &amp;amp; Regards,\nMahindra Defence"'&lt;BR /&gt;/ ' },'&lt;BR /&gt;/ ' "toRecipients": ['&lt;BR /&gt;/ ' { "emailAddress": { "address": "rahane.satish@mahindra.com" } }'&lt;BR /&gt;/ ' ],'&lt;BR /&gt;/ ' "attachments": ['&lt;BR /&gt;/ ' {'&lt;BR /&gt;/ ' "@odata.type": "#microsoft.graph.fileAttachment",'&lt;BR /&gt;/ ' "name": "DO Efficiency Report.xlsx",'&lt;BR /&gt;/ ' "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",'&lt;BR /&gt;/ ' "contentBytes": "' b64_val +(-1) '"'&lt;BR /&gt;/ ' }'&lt;BR /&gt;/ ' ]'&lt;BR /&gt;/ ' }'&lt;BR /&gt;/ '}';&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;/* 4. Send the mail */&lt;BR /&gt;filename resp temp;&lt;BR /&gt;proc http&lt;BR /&gt;method="POST"&lt;BR /&gt;url="&lt;A href="https://graph.microsoft.com/v1.0/me/sendMail" target="_blank"&gt;https://graph.microsoft.com/v1.0/me/sendMail&lt;/A&gt;"&lt;BR /&gt;in=json_req&lt;BR /&gt;out=resp;&lt;BR /&gt;headers&lt;BR /&gt;"Authorization"="Bearer &amp;amp;access_token" /* Ensure this matches your token variable name */&lt;BR /&gt;"Content-Type"="application/json";&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;/* 5. Check log for Success */&lt;BR /&gt;data _null_;&lt;BR /&gt;infile resp;&lt;BR /&gt;input;&lt;BR /&gt;put _infile_;&lt;BR /&gt;run;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Tue, 30 Dec 2025 12:08:39 GMT</pubDate>
    <dc:creator>SatishR</dc:creator>
    <dc:date>2025-12-30T12:08:39Z</dc:date>
    <item>
      <title>Regarding Conversion of Binary to Base64 for Mail attachemnt</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regarding-Conversion-of-Binary-to-Base64-for-Mail-attachemnt/m-p/981365#M379092</link>
      <description>&lt;DIV class="x_elementToProof" data-olk-copy-source="MessageBody"&gt;We have successfully migrated our SAS email functionality from SMTP with Basic Authentication to an OAuth2-based approach using Microsoft Azure Portal and Microsoft Graph API.&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;Currently, we are able to trigger emails successfully using OAuth2. However, we are facing an issue while sending&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Excel file attachments&lt;/STRONG&gt;.&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;&lt;SPAN&gt;As part of the OAuth2-based email implementation, we are converting the Excel (.xlsx) file into Base64 format and attaching it using the&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;content Bytes&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;attribute in the Graph API request. Although the email is delivered successfully with the attachment, the Excel file fails to open when downloaded from the email and shows an error indicating that the file is corrupted or the format is invalid.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SatishR_0-1767096495653.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/112454i5AC502C43EF1FC36/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SatishR_0-1767096495653.png" alt="SatishR_0-1767096495653.png" /&gt;&lt;/span&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV class="x_elementToProof"&gt;We have attached the following for your reference:&lt;/DIV&gt;
&lt;UL data-spread="false"&gt;
&lt;LI&gt;
&lt;DIV class="x_elementToProof" role="presentation"&gt;SAS code used for converting the Excel file into Base64&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class="x_elementToProof" role="presentation"&gt;SAS log generated during the Base64 conversion and email trigger process&lt;BR /&gt;&lt;BR /&gt;
&lt;P&gt;/* 1. Set options for long strings */&lt;BR /&gt;options noquotelenmax;&lt;BR /&gt;%let report_date = %sysfunc(intnx(day,%sysfunc(today()),-1),date11.);&lt;/P&gt;
&lt;P&gt;/* 2. Define filenames */&lt;BR /&gt;filename excel_in "/home/sas/DO Efficiency Report.xlsx" recfm=n;&lt;BR /&gt;filename json_req temp lrecl=32767;&lt;/P&gt;
&lt;P&gt;/* 3. Convert Excel to Base64 and build JSON */&lt;BR /&gt;data _null_;&lt;BR /&gt;file json_req;&lt;BR /&gt;&lt;BR /&gt;infile excel_in;&lt;BR /&gt;length file_content $10547; &lt;BR /&gt;input file_content $char10547.;&lt;BR /&gt;&lt;BR /&gt;length b64_val $14064; &lt;BR /&gt;b64_val = put(file_content, $base64x14064.);&lt;/P&gt;
&lt;P&gt;/* Build the JSON payload with your specific draft */&lt;BR /&gt;put '{'&lt;BR /&gt;/ ' "message": {'&lt;BR /&gt;/ ' "subject": "Daily Reports of &amp;amp;report_date",'&lt;BR /&gt;/ ' "body": {'&lt;BR /&gt;/ ' "contentType": "Text",'&lt;BR /&gt;/ ' "content": "Dear Sir,\n\nPlease find below daily reports of &amp;amp;report_date for your reference,\n\n1. Call Summary Report (Rural &amp;amp; Urban)\n2. Call Summary Report\n3. Do Efficiency Report\n4. ERV Efficiency Report\n5. ERV Tracking Report\n6. Women Related Call summary\n\nThanks &amp;amp; Regards,\nMahindra Defence"'&lt;BR /&gt;/ ' },'&lt;BR /&gt;/ ' "toRecipients": ['&lt;BR /&gt;/ ' { "emailAddress": { "address": "rahane.satish@mahindra.com" } }'&lt;BR /&gt;/ ' ],'&lt;BR /&gt;/ ' "attachments": ['&lt;BR /&gt;/ ' {'&lt;BR /&gt;/ ' "@odata.type": "#microsoft.graph.fileAttachment",'&lt;BR /&gt;/ ' "name": "DO Efficiency Report.xlsx",'&lt;BR /&gt;/ ' "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",'&lt;BR /&gt;/ ' "contentBytes": "' b64_val +(-1) '"'&lt;BR /&gt;/ ' }'&lt;BR /&gt;/ ' ]'&lt;BR /&gt;/ ' }'&lt;BR /&gt;/ '}';&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;/* 4. Send the mail */&lt;BR /&gt;filename resp temp;&lt;BR /&gt;proc http&lt;BR /&gt;method="POST"&lt;BR /&gt;url="&lt;A href="https://graph.microsoft.com/v1.0/me/sendMail" target="_blank"&gt;https://graph.microsoft.com/v1.0/me/sendMail&lt;/A&gt;"&lt;BR /&gt;in=json_req&lt;BR /&gt;out=resp;&lt;BR /&gt;headers&lt;BR /&gt;"Authorization"="Bearer &amp;amp;access_token" /* Ensure this matches your token variable name */&lt;BR /&gt;"Content-Type"="application/json";&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;/* 5. Check log for Success */&lt;BR /&gt;data _null_;&lt;BR /&gt;infile resp;&lt;BR /&gt;input;&lt;BR /&gt;put _infile_;&lt;BR /&gt;run;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Tue, 30 Dec 2025 12:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regarding-Conversion-of-Binary-to-Base64-for-Mail-attachemnt/m-p/981365#M379092</guid>
      <dc:creator>SatishR</dc:creator>
      <dc:date>2025-12-30T12:08:39Z</dc:date>
    </item>
  </channel>
</rss>

