I'm trying to get a "stream" object through an api, and then upload it to a message that will be sent by mail. The whole process WORKS, but the problem is when viewing the documents, because when I enter the inbox of any of the recipients, and try to preview or download and open them from any PDF reader, I get the following message :
There was an error loading the PDF document.
I do not know if the loss of the content of the document is given at the time of making the request to the api or when I send the mail (which is by smtp, authenticated with oAuth2). Can someone help me? Below I show the key fragments of my source code:
App.Sell.Models.DocDTO.DocTypes docType = (App.Sell.Models.DocDTO.DocTypes)Session["AttachDocType"];
string docEntry = (string)Session["AttachDocEntry"];
string docNum = (string)Session["AttachDocNum"];
using (var cl = new HttpClient())
{
//cl.BaseAddress = new Uri("http://localhost:xxxxx/");
cl.BaseAddress = new Uri(ServiceRoute.cnString() + "MyWsOneSell/");
cl.DefaultRequestHeaders.Accept.Clear();
cl.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var s = cl
.GetAsync("api/PdfDocument/GetStreamDocument/" + c.DBName + "/" + docType + "/" + docEntry)
.Result
.Content.ReadAsStreamAsync().Result;
mailMessage.Attachments.Add(new System.Net.Mail.Attachment(s, docType + docNum + ".pdf", "application/pdf"));
}
And here is where I sent the email:
var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage);
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
var oauth2 = new SaslMechanismOAuth2(Email.From, Token.AccessToken);
client.Authenticate(oauth2);
try
{
client.Send(mimeMessage);
... rest of code.
}
...