How would you attach an excel document using MailJet api - email

Hello I have tried using mailjets api to send an excel document.
I first encode the excel file to base64 using mailjets library.
I then use mailjets library to add the file as an attachment and set the mime type application/vnd.ms-excel.
Heres an example.
request = new MailjetRequest(Emailv31.resource)
.property(Emailv31.MESSAGES, new JSONArray()
.put(new JSONObject()
.put(Emailv31.Message.FROM, new JSONObject()
.put("Email", "pilot#mailjet.com")
.put("Name", "Mailjet Pilot"))
.put(Emailv31.Message.TO, new JSONArray()
.put(new JSONObject()
.put("Email", "passenger1#mailjet.com")
.put("Name", "passenger 1")))
.put(Emailv31.Message.SUBJECT, "Your email flight plan!")
.put(Emailv31.Message.TEXTPART, "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!")
.put(Emailv31.Message.HTMLPART, "<h3>Dear passenger 1, welcome to Mailjet!</h3><br />May the delivery force be with you!")
.put(Emailv31.Message.ATTACHMENTS, new JSONArray()
.put(new JSONObject()
.put("ContentType", "application/vnd.ms-excel")
.put("Filename", "test.txt")
.put("Base64Content", "excelFileBase64")))));
response = client.post(request);
However I always receive the file as its string represented base64 encoded.

Use ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
and test.xlsx

Related

How can I get a JWT Access Token using Docusign 4.0.4.0

I have a legacy application written in VB6 (I Know!!) to which I am adding a DocuSign feature. I a using InterOp to run .NET code written is VS2019 VB.Net. The reason I am using 4.0.4.0 as this is the version I can successfully call using InterOp. I have all the code working except for requesting a JWT token. Does anyone have JWT code working under this version of the API ?
This is what I have working under later versions of DocuSign and am trying to replace
`Dim privatekey As Byte() = File.ReadAllBytes("docusign_private_key.key")
Dim result As Object = x.RequestJWTApplicationToken(gsSIGNERCLIENTID, docuSignAuthServer, privatekey, 1, scopes)
txtAccessToken.Text = result
'
' Second attempt at alternative
'
` Dim authToken As OAuth.OAuthToken = x.RequestJWTUserToken(gsSIGNERCLIENTID, gsUserIDGUID, docuSignAuthServer, privatekey, 1)
txtAccessToken.Text = authToken.access_token`
'
' Third attempt
'
Dim request = TryCast(System.Net.WebRequest.Create("https://account-d.docusign.com/oauth/token"), System.Net.HttpWebRequest)
request.Method = "POST"
request.Headers.Add("username", "user123")
request.Headers.Add("password", "123")
request.Headers.Add("auth_access_type", "read")
request.ContentLength = 0
Dim responseContent As String = ""
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
End Using
End Using
'
' Fifth attempt
'
Dim apiClient5 As New ApiClient(gsBASEPATH)
Dim authToken5 As OAuth.OAuthToken = apiClient5.RequestJWTUserToken(gsINTEGRATIONKEY, gsUserIDGUID, "https://account-d.docusign.com/oauth/token", privatekey, 1, scopes)
All results in 'Error while requesting server, received a non-successful HTPC code Error with response Body'
Your second attempt is correct:
Dim authToken As OAuth.OAuthToken = x.RequestJWTUserToken(gsSIGNERCLIENTID, gsUserIDGUID, docuSignAuthServer, privatekey, 1)
txtAccessToken.Text = authToken.access_token
AuthServer has to be "account-d.docusign.com"
The privateKey has to be the exact thing you got from DocuSign, including the new lines and all. To get it use this code:
File.ReadAllBytes(path) where path is the path on the disk to a plain text file with your private key.
gsSIGNERCLIENTID should be the GUID for the user that you use that has consent. You must make sure user gave consent by building a consent URL as explained in here:
https://developers.docusign.com/platform/auth/consent/obtaining-individual-consent/
If you did all of this correctly - it should work.

Curl Post request to upload .zip file to Sharepoint returning "Compressed (zipped) folder invalid when downloading

I have a curl Post request that attempts to upload the latest documents to our Sharepoint. It appears to work fine, the file I send is valid and opens fine before I send it. Once it arrives on sharepoint the filesize is the same but when I download it appears to be corrupted and wont open.
I have tried manually uploading the zip file to Sharepoint, it works fine this way.
I have also scanned the internet for some solution to the problem but I am unable to find anything.
curl -X POST
"http://12.3.456.78:8080/TEST-2.0/sharepoint?relativePath=Shared%20Documents%2FRelease%20Documents%2Fv1&teamSite=Test"
-H "accept: /" -H "Content-Type: multipart/form-data" -F "file=#Test.zip;type=application/x-zip-compressed"
I expect the file on sharepoint to be valid and open correctly after download but I get the following error.
"Windows cannot open the folder.
The Compressed (zipped) Folder 'C:\Test.zip' is invalid.'
The answer was within the code, although the file was sending, it was obviously not sending correctly which was causing the corruption.
The line that I added that made the difference was the following.
post.setEntity( new FileEntity(file) );
My full method can be found below, feel free to comment if anyone needs further information on this in the future.
public HttpResponse uploadFile( String teamSite,
String relativePath,
String accessToken,
File file ) throws IOException
{
String fileName = null;
HttpClient client = HttpClientBuilder.create().build();
fileName = file.getName().replaceAll( " ", "%20" );
relativePath = relativePath.replaceAll( " ", "%20" );
teamSite = teamSite.replaceAll( " ", "%20" );
HttpPost post = new HttpPost( URL );
post.setHeader( "Accept", "application/json;odata=verbose" );
post.setHeader( "Authorization", "Bearer " + accessToken );
post.setEntity( new FileEntity(file) ); //This line here
HttpResponse response = client.execute( post );
HttpEntity entity = response.getEntity();
#SuppressWarnings("unused")
String responseString = EntityUtils.toString( entity, "UTF-8" );
return response;
}

Sending an email from scala - Authentication failed exception

I am trying to send an email(gmail) from scala. Here is what I have so far-
import javax.mail._
import javax.mail.internet._
// Get the user's message
var bodyText = "Hello World!"
// Set up the mail object
val props = System.getProperties
props.setProperty("mail.smtp.host", "smtp.gmail.com")
props.setProperty("mail.smtp.user","user");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.debug", "true");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable","true");
props.setProperty("mail.smtp.EnableSSL.enable","true");
val session = Session.getInstance(props)
val message = new MimeMessage(session)
// Set the from, to, subject, body text
message.setFrom(new InternetAddress("myemail#gmail.com"))
message.setRecipients(Message.RecipientType.TO, "myemail#gmail.com")
message.setSubject("First email")
message.setText(bodyText)
// And send it
Transport.send(message)
The error that I am getting is just
javax.mail.AuthenticationFailedException
I understand that I may need to provide a password somewhere but I am following the tutorial http://langref.org/scala/networking/smtp/send-an-email and they don't require password.
According to http://www.oracle.com/technetwork/java/faq-135477.html#smtpauth, you should use Transport.send(message, user, password)

Invoke A Multipart REST Service with vaadin

I need to send a Multipart file data to REST service from vaadin . How can I achieve it ? .. ( web service API is listed below)
#RequestMapping(value="/upload", method=RequestMethod.POST)
public #ResponseBody String[] handleFileUpload(
#RequestParam( value="file" , required=false) MultipartFile file , #RequestParam( value="title" , required=false)String title ,#RequestParam( value="description" , required=false)String description ){
// file uploading logic....
}
When working with external HTTP based services in Java / VAADIN I'm ussualy using very nice JODD Java library specificaly http://jodd.org/doc/http.html
To post attachment to URL as explained in question, simply use something like this:
HttpRequest httpRequest = HttpRequest
.post("http://server:8080/upload")
.form(
"file", new File("c:\\a.jpg.zip")
);
HttpResponse httpResponse = httpRequest.send();
HttpRequest is object from JODD library. You can include JODD into maven config e.g. http://jodd.org/download/

Writing an encrypted mail via Exchange Web Services

I would like to send an encrypted EMail Message with Exchange WEb Services using C#.
Is there any possibillity?
Thanks
Edit:
My Mail body encrypter:
public static byte[] encry(string body, ContentTyp typ, string to )
{
X509Certificate2 cert = GetMailCertificate(to);
StringBuilder msg = new StringBuilder();
msg.AppendLine(string.Format("Content-Type: text/{0}; charset=\"iso-8859-1\"", typ.ToString()));
msg.AppendLine("Content-Transfer-Encoding: 7bit");
msg.AppendLine();
msg.AppendLine(body);
EnvelopedCms envelope = new EnvelopedCms(new ContentInfo(Encoding.UTF8.GetBytes(msg.ToString())));
CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, cert);
envelope.Encrypt(recipient);
//System.IO.MemoryStream ms = new System.IO.MemoryStream(envelope.Encode());
return envelope.Encode();
}
Main
byte [] con = encrypted.encry("test", encrypted.ContentTyp.plain, "test#server.com");
EmailMessage msg1 = new EmailMessage(_server);
msg1.MimeContent = new MimeContent("UTF-8", con);
msg1.ToRecipients.Add("user#server.com");
msg1.InternetMessageHeaders = ??
msg1.Send();
If you are referring to S/Mime encryption, then you'll have to create the encrypted message according to RFC 3852 and RFC 4134. After you've done that, you can send the message.
Using the EWS Managed API, this can be done as follows:
var item = new EmailMessage(service);
item.MimeContent = new MimeContent(Encoding.ASCII.HeaderName, content);
// Set recipient infos, etc.
item.Send();
EDIT:
You should add the standard headers like From, To, Date, Subject, etc. And the content-type.
Subject: Test
From: "sender" <sender#yourcompany.com>
To: "recipient" <recipient#othercompany.com>
Content-Type: application/pkcs7-mime; smime-type=signed-data; name=smime.p7m
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=smime.p7m
Your encrypted body goes here
Just use a StringWriter put all that together. The result is your MIME body.