I'm currently developing an application which download attachment from gmail account.
Right now, I got error whenever downloading zipped attachment. But, not all, some I can retrieve it without error. Here's the Exception message:
Exception in thread "main" com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 1 before EOF, the 10 most recent characters were: "Q3w5ilxj2P"
FYI: I was able to download the attachment via gmail web interface.
Here's the snippet:
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.getFileName().toLowerCase().endsWith("zip") ||
bodyPart.getFileName().toLowerCase().endsWith("rar")) {
InputStream is = bodyPart.getInputStream();
File f = new File("/tmp/" + bodyPart.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[bodyPart.getSize()];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
}
}
}
Anyone have idea, how to work around this problem?
From a list of the known limitations, bugs, issues of JavaMail:
Certain IMAP servers do not implement
the IMAP Partial FETCH functionality
properly. This problem typically
manifests as corrupt email attachments
when downloading large messages from
the IMAP server. To workaround this
server bug, set the
"mail.imap.partialfetch" property to
false. You'll have to set this
property in the Properties object that
you provide to your Session.
So you should just turn off partial fetch in imap session. For example:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.partialfetch", "false");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>","<password>");
source: https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html
If You Are Using java mail API then add these lines while you are connectin the imap server......
Properties prop = new Properties();
prop.put("mail.imaps.partialfetch", false);
Session session = Session.getDefaultInstance(prop, null);
........
.... your code ..
......
it should work.
Related
I'm looking for a good example which explains how to download files from the server side of a web app created with Struts 1.1 and JSP;
I tried to follow some tips like this:
Best download file with struts 1.1 method
https://www.mkyong.com/struts/struts-download-file-from-website-example/
I can create the file, put it in the HttpServletResponse and correctly open it through the message that appears from the browser (I have to use IE11, unfortunately), but after opening (or closing) the .xls file, the page remains locked while loading.
I have found that the page remains blocked as soon as the message opening the excel file appears.
This is part of my code in the Action:
OutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=ExcelSheet.xls");
FileInputStream in = new FileInputStream("ExcelSheet.xls");
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
//others insignificant lines of code
return null;
and this is the message that appears on the browser:
browserMessage
As soon as this message appears, the page keeps loading infinitely.
How can I stop the infinitely loading?
I'm implementing a domino web service provider, whose purpose is to stream from the base64 format, which in the client that consumes the web service is an attachment file, transform it back into a file. In the web service provider that is developed in java, I use the Stream class and the Mime classes to, to convert the stream and file. The web service provider works well for files up to 5 MB, for larger files the error as technote is displayed. Has anyone had this problem yet? Is there any way around it?
Here is the code for the web service provider
public class criaAnexo {
private Vector itemsToRecycle;
public void attachDocument( byte[] is) {
// creating the output stream used to create the MIME attachments
try
{
itemsToRecycle = new Vector();
Session session = NotesFactory.createSession();
Database db = session.getDatabase("Serverx", "base.nsf");
if (!db.isOpen())
System.out.println("names2.nsf does not exist on snapper");
else
{
Stream outStream = session.createStream();
outStream.write(is);
session.setConvertMIME(false);
// create the MIME body
Document doc = db.createDocument();
doc.replaceItemValue("Form", "formAttachment");
MIMEEntity body = doc.createMIMEEntity();
// create a child for each attachment<br/>
MIMEEntity child = body.createChildEntity();
// find the fileSuffix<br/>
//String fileSuffix = files[i].substring(files[i].lastIndexOf(".")+1);
String fileSuffix = "pdf";
// set the child to the outstream using a mapped MIME type<br/>
// MIME type mapping see: http://www.w3schools.com/media/media_mimeref.asp
//child.setContentFromBytes(outStream, mapMIMEType(fileSuffix), MIMEEntity.ENC_IDENTITY_BINARY);
child.setContentFromBytes(outStream, "application/pdf", MIMEEntity.ENC_IDENTITY_BINARY);
// set name for file attachment<br/>
MIMEHeader header = child.createHeader("Content-Disposition");
header.setHeaderVal("attachment; filename=\"teste.pdf\"");
// set unique id for file attachment to be able to refer to it<br/>
header = child.createHeader("Content-ID");
header.setHeaderVal("teste.pdf");
//outStream.truncate();
//outStream.close();
outStream.close();
Runtime rt = Runtime.getRuntime();
long total_mem = rt.totalMemory();
long free_mem = rt.freeMemory();
long used_mem = total_mem - free_mem;
System.out.println("Total de Memória:"+total_mem);
System.out.println("Total de Memória livre:"+free_mem);
System.out.println("Total de memoria usada pelo agente: " + used_mem/1048576);
doc.save( true, true );
itemsToRecycle.add(doc);
session.recycle(itemsToRecycle); //recycle all items added to vector
session.recycle();
}
}
catch(Exception e)
{
}
}
}
Due to base64 encoding and other overhead, files larger than 5 MB may be exceeding the 10 MB limits you have configured for the Maximum size of request content and Maximum POST data settings for your server. Try increasing them.
In fact the limitation occurs in the client that consumes the web service that I implemented in the domino itself. The technote quoted in the description of the problem, implies that the problem is on the side of the provider, but in fact it is not. When I implemented the web service client on dot net the file was streamed without problems.
I can't use the ServiceStack Client libraries and I've chosen to use the HttpClient PCL library instead. I can do all my Rest calls (and other json calls) without a problem, but I'm now stucked with uploading files.
A snippet of what I am trying to do:
var message = new HttpRequestMessage(restRequest.Method, restRequest.GetResourceUri(BaseUrl));
var content = new MultipartFormDataContent();
foreach (var file in files)
{
byte[] data;
bool success = CxFileStorage.TryReadBinaryFile(file, out data);
if (success)
{
var byteContent = new ByteArrayContent(data);
byteContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = System.IO.Path.GetFileName(file) ,
};
content.Add(byteContent);
}
}
message.Content = content;
Problem is now that I get a null reference exception (status 500) when posting. I doesn't get into the service. I see the call in the filterrequest, but that's it.
So I'm wondering what I do wrong and how I can pinpoint what is going wrong. How can I catch the correct error on the ServiceStack layer?
The web socket is written in javascript by my colleague. I managed to connect. First of all I have to log in on the application using a test account. I have to send the email and password through a json. I have installed the Json.Net packet using NuGet.
Some code that I found on my reaserch is this, but I do not understand how to send my data using that segment.
var buffer = new byte[1024];
var segment = new ArraySegment<byte>(buffer);
webSocket.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None);
Of course, I can use an object
User user=new User();
user.Email="bla#bla.com";
user.Password="pass";
string json = JsonConvert.SerializeObject(user);
But it will not be of any use because the method SendAsync accepts only byte type on segment.
All I want is to send that data, and if log in succeeds, I should receive other data (in Json format) about the user.
As a side note, I am quite new to web sockets, I used http protocols from ASP.NET WEB API 2.
I have no idea about Windows Phone 8, but by the code you pasted it seems similar to the regular .NET ClientWebSocket, so here you have some examples:
public static Task SendString(ClientWebSocket ws, String data, CancellationToken cancellation)
{
var encoded = Encoding.UTF8.GetBytes(data);
var buffer = new ArraySegment<Byte>(encoded, 0, encoded.Length);
return ws.SendAsync(buffer, WebSocketMessageType.Text, true, cancellation);
}
public static async Task<String> ReadString(ClientWebSocket ws)
{
ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]);
WebSocketReceiveResult result = null;
using (var ms = new MemoryStream())
{
do
{
result = await ws.ReceiveAsync(buffer, CancellationToken.None);
ms.Write(buffer.Array, buffer.Offset, result.Count);
}
while (!result.EndOfMessage);
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(ms, Encoding.UTF8))
return reader.ReadToEnd();
}
}
If something does not compile or exists in WP8, just find an equivalent.
#vtortola is a working example in case your data comes in multiple segmented messages, but if all data comes in a single message you don't need all those streams to read the message, you just need to do this:
public static async Task<String> ReadString(ClientWebSocket socket)
{
var reciveBuffer = new byte[32000];
var result = await socket.ReceiveAsync(new ArraySegment<byte>(reciveBuffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
return Encoding.ASCII.GetString(reciveBuffer , 0, result.Count);
}
If your message is splited in multiple segments or you don't know how your message is comming then you have to do like #vtortola
Also if you want to keep receiving messages you can do a while and call ReadString inside, like this:
while (socket.State == WebSocketState.Open)
{
var msg = ReadString(socket)
//do something with your message...
}
This is my first time ever with Sharepoint. Here is the scenario
I have a stand alone web application
I also have a stand alone sharepoint server.
Both are on different servers.
I need to upload a file from web application to sharepoint
I found 2 methods online,
Using the webservice provided by Sharepoint (CopyIntoItems)
Using jQuery library of Sharepoint webservice
After searching the web, I think the jQuery part will not work (you can correct me).
I am looking for a method that takes username/password and uploads a pdf file to Sharepoint server. The following is my C# code that tries to upload but ends up in error
public bool UploadFile(string file, string destination)
{
bool success = false;
CopySoapClient client = new CopySoapClient();
if (client.ClientCredentials != null)
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
try
{
client.Open();
string filename = Path.GetFileName(file);
string destinationUrl = destination + filename;
string[] destinationUrls = { destinationUrl };
FieldInformation i1 = new FieldInformation { DisplayName = "Title", InternalName = "Title", Type = FieldType.Text, Value = filename };
FieldInformation[] info = { i1 };
CopyResult[] result;
byte[] data = File.ReadAllBytes(file);
//uint ret = client.CopyIntoItems(filename, destinationUrls, info, data, out result);
uint ret = client.CopyIntoItems(file, destinationUrls, info, data, out result);
if (result != null && result.Length > 0 && result[0].ErrorCode == 0)
success = true;
}
finally
{
if (client.State == System.ServiceModel.CommunicationState.Faulted)
client.Abort();
if (client.State != System.ServiceModel.CommunicationState.Closed)
client.Close();
}
return success;
}
I am calling the above function like this
UploadFile(#"C:\temp\uploadFile.txt", "http://spf-03:300/demo/Dokumente").ToString();
Error that i get:
Error Code: Destination Invalid
Error Message: The service method 'Copy' must be called on the same domain that contains the target URL.
There is the 3rd option with SharePoint 2010 and that is to use the Client Side object model. The client side object model a a sub set of the larger Sharepoint API, but it does cover uploading documents. Below is blog post with an example of uploading.
Upload document through client object model
As with most things in SharePoint you will need to authenticate against it the site, so find out if your site collection is forms based or claims based and then you should be able to find sample code for your situation.
Solution to the problem:
The problem was that the "security token webservice" was not working and it was giving some error when we manually ran the webservice.
The server was unable to process the request due to an internal error.
For more information about the error, either turn on
IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute
or from the configuration behavior) on the server in order to send the
exception information back to the client, or turn on tracing as per
the Microsoft .NET Framework 3.0 SDK documentation and inspect the
server trace logs.
The above exception is a generic one. To view the exact exception we enabled remote error viewing from the web.config file of the webservice(link) and saw the exact exception.
We found the solution for the exception and the service started. After that everything was working fine.