<?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 Sending emails from SAS Viay4 on AWS using Oauth2 and Microsoft graph and Excel files as attachment in SAS Viya</title>
    <link>https://communities.sas.com/t5/SAS-Viya/Sending-emails-from-SAS-Viay4-on-AWS-using-Oauth2-and-Microsoft/m-p/985956#M3054</link>
    <description>&lt;P&gt;Hello&lt;BR /&gt;I have been trying to send emails with excel files as attachment using SAS code from SAS Viya4 on AWS.&lt;BR /&gt;Sending of email using Oauth2 and Microsoft graph is the method we are expected to use.&lt;BR /&gt;(SMTP approach is no more allowed).&lt;BR /&gt;One can send emails but when an excel file is attached, it file is restricted to 270 bytes. The file is simply unusable.&lt;BR /&gt;The relevant portion of the code are attached.&lt;BR /&gt;There are no error in the log and get the message "NOTE : 202 ACCEPTED.&lt;BR /&gt;Request the community for guidance.&lt;BR /&gt;The code is as given below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* Step 1: Base64 encode the XLSX file */
filename myfile "/mnt/viya-share/data/test/cars.xlsx";
filename b64file temp;

data _null_;
    infile myfile recfm=n;
    file b64file lrecl=32767;
    input byte $char1.;
    put byte $base64x1. @@;  
run;
/* Step 2: Build JSON email payload */
filename mailbdy temp;

/* Write JSON header */
data _null_;
    file mailbdy;
    put '{';
    put '  "message": {';
    put '    "subject": "Test email from SAS Viya 4",';
    put '    "body": { "contentType": "Text", "content": "Hello from SAS Viya!" },';
    put '    "toRecipients": [ { "emailAddress": { "address": "recipient@example.com" } } ],';
    put '    "attachments": [';
    put '      {';
    put '        "@odata.type": "#microsoft.graph.fileAttachment",';
    put '        "name": "cars.xlsx",';
    put '        "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",';
    put '        "contentBytes": "';
run;

/* Append Base64 content directly */
data _null_;
    infile b64file lrecl=32767;
    file mailbdy mod;
    input;
    put _infile_;
run;

/* Close JSON */
data _null_;
    file mailbdy mod;
    put '"';
    put '      }';
    put '    ]';
    put '  },';
    put '  "saveToSentItems": "true"';
    put '}';
run;

/* Send via microsoft Graph */
/* Step 3: Send email via Microsoft Graph */
filename sendresp temp;

proc http
    url="https://graph.microsoft.com/v1.0/users/sender@example.com/sendMail"
    method="POST"
    in=mailbdy
    out=sendresp;
    headers
        "Authorization"="Bearer &amp;amp;access_token"
        "Content-Type"="application/json";
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 03 Apr 2026 21:11:29 GMT</pubDate>
    <dc:creator>thesasuser</dc:creator>
    <dc:date>2026-04-03T21:11:29Z</dc:date>
    <item>
      <title>Sending emails from SAS Viay4 on AWS using Oauth2 and Microsoft graph and Excel files as attachment</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Sending-emails-from-SAS-Viay4-on-AWS-using-Oauth2-and-Microsoft/m-p/985956#M3054</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;I have been trying to send emails with excel files as attachment using SAS code from SAS Viya4 on AWS.&lt;BR /&gt;Sending of email using Oauth2 and Microsoft graph is the method we are expected to use.&lt;BR /&gt;(SMTP approach is no more allowed).&lt;BR /&gt;One can send emails but when an excel file is attached, it file is restricted to 270 bytes. The file is simply unusable.&lt;BR /&gt;The relevant portion of the code are attached.&lt;BR /&gt;There are no error in the log and get the message "NOTE : 202 ACCEPTED.&lt;BR /&gt;Request the community for guidance.&lt;BR /&gt;The code is as given below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* Step 1: Base64 encode the XLSX file */
filename myfile "/mnt/viya-share/data/test/cars.xlsx";
filename b64file temp;

data _null_;
    infile myfile recfm=n;
    file b64file lrecl=32767;
    input byte $char1.;
    put byte $base64x1. @@;  
run;
/* Step 2: Build JSON email payload */
filename mailbdy temp;

/* Write JSON header */
data _null_;
    file mailbdy;
    put '{';
    put '  "message": {';
    put '    "subject": "Test email from SAS Viya 4",';
    put '    "body": { "contentType": "Text", "content": "Hello from SAS Viya!" },';
    put '    "toRecipients": [ { "emailAddress": { "address": "recipient@example.com" } } ],';
    put '    "attachments": [';
    put '      {';
    put '        "@odata.type": "#microsoft.graph.fileAttachment",';
    put '        "name": "cars.xlsx",';
    put '        "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",';
    put '        "contentBytes": "';
run;

/* Append Base64 content directly */
data _null_;
    infile b64file lrecl=32767;
    file mailbdy mod;
    input;
    put _infile_;
run;

/* Close JSON */
data _null_;
    file mailbdy mod;
    put '"';
    put '      }';
    put '    ]';
    put '  },';
    put '  "saveToSentItems": "true"';
    put '}';
run;

/* Send via microsoft Graph */
/* Step 3: Send email via Microsoft Graph */
filename sendresp temp;

proc http
    url="https://graph.microsoft.com/v1.0/users/sender@example.com/sendMail"
    method="POST"
    in=mailbdy
    out=sendresp;
    headers
        "Authorization"="Bearer &amp;amp;access_token"
        "Content-Type"="application/json";
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2026 21:11:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Sending-emails-from-SAS-Viay4-on-AWS-using-Oauth2-and-Microsoft/m-p/985956#M3054</guid>
      <dc:creator>thesasuser</dc:creator>
      <dc:date>2026-04-03T21:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: Sending emails from SAS Viay4 on AWS using Oauth2 and Microsoft graph and Excel files as attachm</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Sending-emails-from-SAS-Viay4-on-AWS-using-Oauth2-and-Microsoft/m-p/986024#M3055</link>
      <description>I tested your DATA step code on a file and it did not produce base64 encoded content.&lt;BR /&gt;&lt;BR /&gt;I found this blog post that provides some code to base64 encode an entire file using the base64 format.&lt;BR /&gt;&lt;A href="https://support.selerity.com.au/hc/en-us/articles/223345708-Tip-SAS-and-Base64" target="_blank"&gt;https://support.selerity.com.au/hc/en-us/articles/223345708-Tip-SAS-and-Base64&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Does using that code to encode your file allow the attachment to send as expected?</description>
      <pubDate>Mon, 06 Apr 2026 13:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Sending-emails-from-SAS-Viay4-on-AWS-using-Oauth2-and-Microsoft/m-p/986024#M3055</guid>
      <dc:creator>gwootton</dc:creator>
      <dc:date>2026-04-06T13:46:51Z</dc:date>
    </item>
  </channel>
</rss>

