Reputation: 6676
I am writing class for Windows Store application that can connect to imap.gmail.com, find specific message and download email attachment from it. I decided to write my own class, because could not find any ready solution for Windows Store.
I have problem even with connecting imap server, I use StreamSocket and can not understand, what am I doing wrong. Here is my code:
public sealed class GmailAttachmentDownloader
{
private HostName HostName;
private StreamSocket Socket;
private String AccessToken;
private String LoginName;
private DataWriter Writer;
private DataReader Reader;
public GmailAttachmentDownloader(String loginName, String accessToken)
{
HostName = new HostName("imap.gmail.com");
LoginName = loginName;
AccessToken = accessToken;
}
private async Task<bool> Connect()
{
Socket = new StreamSocket();
try
{
await Socket.ConnectAsync(HostName, "993", SocketProtectionLevel.SslAllowNullEncryption);
}
catch
{
return false;
}
return true;
}
private async Task<bool> Write(String msg)
{
if (Socket == null)
if (! await Connect())
throw new Exception("Could not connect to imap.gmail.com");
try {
using (Writer = new DataWriter(Socket.OutputStream))
{
Writer.WriteString(msg);
await Writer.StoreAsync();
}
}
catch
{
return false;
}
return true;
}
private async Task<String> Read()
{
if (Socket == null)
if (!await Connect())
throw new Exception("Could not connect to imap.gmail.com");
String data;
using(Reader = new DataReader(Socket.InputStream))
{
Reader.InputStreamOptions = InputStreamOptions.Partial;
await Reader.LoadAsync(1024);
data = Reader.ReadString(Reader.UnconsumedBufferLength);
}
return data;
}
public async void Login()
{
String data;
data = await Read();
await Write("a1 AUTHENTCATE XOAUTH2 " + EncodeTo64("user=" + LoginName + "\x01auth=Bearer " + AccessToken + "\x01\x01\r\n"));
data = await Read();
}
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes
= System.Text.UTF8Encoding.UTF8.GetBytes(toEncode);
string returnValue
= System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
static public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes
= System.Convert.FromBase64String(encodedData);
string returnValue =
System.Text.UTF8Encoding.UTF8.GetString(encodedDataAsBytes, 0, encodedDataAsBytes.Count());
return returnValue;
}
}
Could anyone help me please? I tried searching in web and Microsoft tutorials, also I found this question, but it did not help me much.
UPD: Sorry, after I searched for exception in google, I found, that problem is in antivirus and everything works when I turn antivirus software off. So different question: how to deal with it? I do not want tell my friends "turn off your antivirus, so my program can run"?
Upvotes: 0
Views: 790
Reputation: 7231
Well it seems like your Antivirus has a firewall in there. If it blocks your app and you don't want it blocked, it is misconfigured. Simple as that.
Also, you should be aware that antivirus software for the most part is just to give you a fuzzy warm feeling and - in a corporate environment - to avoid getting sued in case anything happens. In the end it won't really protect you from the nasties. Just sayin... ;)
Upvotes: 1