You are not logged in.
In a new project I need to send mail using OAUTH2. The mail can contain attachments like pdf-files and they need to be Base64 encoded.
I feel lost in all alternatives so I need some help to move forward.
I use TWinHttp.post sending mail. It works good with simple mail.
Delphi-11, WIN10
Offline
I create the email data with the Indy components TIdMessage and TIdMessageBuilderHTML. This is very simple. I send this data with a slightly modified version of the function SendEmail() from the unit SynCrtSock.
idMessage := TIdMessage.Create(Nil);
try
idMessage.Subject := 'New Mail';
idMessage.From.Name := 'Hallo2020';
idMessage.From.Address := 'mail@hallo2020.com';
idMessage.Recipients.EMailAddresses := 'your@gmail.com';
idMessageBuilder := TIdMessageBuilderHTML.Create;
try
idMessageBuilder.Html.Text := StringFromFile('EMail.html');
idMessageBuilder.Attachments.Add('Data.pdf');
idMessageBuilder.FillMessage(idMessage);
finally
idMessageBuilder.Free;
end;
idMessage.SaveToStream(strStream);
SendEmail(..., strStream.DataString)
With best regards
Thomas
Offline
Ok, thank's for an answer but I has to send the mail through a service where everything is built around OAuth2. I have everything working and can send the mail but I need to know how I should do with the files that needs to be attached to the mail.
Indy will not help here.
Delphi-11, WIN10
Offline
If I understood your question correctly, my source code should provide the solution. I only use Indy to create the right format. Just have a look at what happens in my example when you display the result in a memo (Memo1.Text := strStream.DataString).
Here you can find a blog article: New HTML Message Builder class
You should use a more recent Indy version from Github than the one shipped with Delphi before version 10.4.
With best regards
Thomas
Offline
I believe he is referring to sending via HTTP.
Make a Post to upload the file.
In this case, SynCurl can help you.
After @mpv creates a separate unit for SynCurl, you can now use all the features that Curl offers.
Offline
Yes, it's about a http-request.
The service requires a body that contans mailFrom, mailto, message etc. and the attached files with name and contentents encoded in Base64 .
Al is wrapped in a json structure:
{
"SendUser": "senderName@somwhere.on.earth",
"Message": "Hello",
"Subject": "HelloSubject",
"ToRecipients": [
"receiver.theMartian@mons.olympus.on.mars"
],
"CcRecipients": [
"flash@titan.on.saturn"
],
"FileAttachments": [
{
"Name": "spacetraveling.pdf",
"Value": "N7I+GuZ4KclTXP7KXM3YRdh0BfKxnMpOv8s.... } // Filecontents base64-encoded
]
}
Part of the code that calls the sendmail service
var
messageID: sockString;
body: variant;
atm: variant;
begin
if getToken = '' then
result := -1
else begin
TDocVariant.new(body);
body.SendUser := aFrom;
body.Message := aMessage;
body.Subject := aSubject;
body.ToRecipients := _Arr([stringReplace(aMailTo,';',',')]);
body.CcRecipients := _Arr([stringReplace(aCC,';',',')]);
if high(aAttachments) > -1 then begin
TDocVariant.new(atm);
atm.name := aAttachments[0];
.
. read and add file contents here - all attachments should be put into an array and then added to the body.
messageID := TWinHTTP.Post(fOaSec.getMailURL,body,fOaSec.getMailHeader);
Delphi-11, WIN10
Offline
I've looked into thread several times already, but so far I didn't understand the problem you have. I guess others have the same confusion.
Could you specify a particular place, where you have the problem? Reading pdf file, converting to base64, forming 'body', something else?
Offline
Take a look in SynCommons.BinToBase64
Offline
Thank's a lot, I've just solved it this way:
TDocVariant.new(body);
body.SendUser := aFrom;
body.Message := aMessage;
body.Subject := aSubject;
body.ToRecipients := _Arr([stringReplace(aMailTo, ';', ',', [rfReplaceAll, rfIgnoreCase])]);
body.CcRecipients := _Arr([stringReplace(aCC, ';', ',', [rfReplaceAll, rfIgnoreCase])]);
if high(aAttachments) > -1 then
begin
for i := 0 to high(aAttachments) do
begin
TDocVariant.new(atm[i]);
atm[i].name := extractFileName(aAttachments[i]);
atm[i].value := fileToBase64(aAttachments[i]);
end;
body.FileAttachments := _Arr([atm]);
end;
messageID := TWinHTTP.Post(fOaSec.getMailURL, body, fOaSec.getMailHeader);
function FileToBase64(const aFileName: RawUTF8): RawUTF8;
var
f: TStream;
bytes: TBytes;
begin
result := '';
f := TFileStream.Create(afilename, fmOpenRead);
try
if f.Size > 0 then
begin
SetLength(bytes, f.Size);
f.Read(bytes[0], f.Size);
end;
result := binToBase64(@bytes[0], f.size); //String(sb);
finally
f.free;
end;
end;
Delphi-11, WIN10
Offline