Get encrypted string when FETCH mail from IMAP - c#

I'm trying to write a IMAP mail client on C#.net with TCPClient and SSL. I can get past the connection and authentication just fine but when I fetch mail from imap.google.com, it return a encrypted string that I can't read at all. Something like:
Subject: =?UTF-8?B?UmU6IDMw5pyf5a6f57+S55Sf44Gu5oOF5aCx44Gr6Zai44GX44Gm?=
My FETCH commands are as below
"$ FETCH " + number + " body[header]\r\n"
"$ FETCH " + number + " body[text]\r\n"
What do I have to do to get the header and body of the mail to display correctly?

Subject: =?UTF-8?B?UmU6IDMw5pyf5a6f57+S55Sf44Gu5oOF5aCx44Gr6Zai44GX44Gm?=
This is not encrypted at all. This is simply the subject encoded using base64 according to the MIME standard, see RFC 2047. Decoding it results in Re: 30期実習生の情報に関して.
In order to deal with such encodings you need to decode it according to the MIME standard, i.e. either read the standard and implement everything yourself or search for some existing library which implements RFC 2047.

Related

'+' character ignored from POST data using c# to webapi2

I have a client that Connects to Asp.net Webapi2,Using Identity & OAuth2 for Authentication.
In Authentication Process , whenever Password Field Contains '+' character.The Server Just Ignore this Character!!!(And Most Other Sign Chars Mentioned In Test below)
string data = "grant_type=password&username=" + username + "&password=" + password;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
data.PostToUrl();//This Is just pseudoCode
In Server Debug:
Sent Data : password=test+1
Received Data : password=test 1
test2
Sent Data : "+_)(&^%$##!~"
Received Data :" _)("
Thanks.
What is the issue? With HTTP URL a + is equivalent to a space. In fact %20 can also be used.
When sending data in a query always use UrlEncode; as in
var q = string.Format("grant_type=password&username={0}&password={1}",
HttpUtility.UrlEncode(username),
HttpUtility.UrlEncode(password));
HttpServerUtility.UrlEncode
this will help solve the problem with special characters such as + anad #
To use it you'll need to add a reference to System.Web (Project Explorer > References > Add reference > System.Web)
Once you've done that you can use it to encode any items you wish

How To Add Mandrill Metadata Using C#

We currently use Mandrill to send our emails and this is working OK.
Now we wish to capture Mandrill webhooks back into our server and annotate the appropriate email source record with the event information. In order to do this we believe we have to pass the ID of the original message to the webhook so that we can pull up that message from our server when the webhook arrives.
We believe the correct approach is to add a Mandrill metadata field to the message, but we're having trouble doing that.
We've tried the following (with variations):
Message.Headers.Add("X-MC-METADATA", "\"OriginId\": \"" + Id + "\"");
but the OriginId is never returned in the webhook and we don't see this header in the email's source. It seems that we don't have quite the right format and that Mandrill is stripping this header out.
The Metadata type accepts arrays so try wrapping the metadata with {}
Message.Headers.Add("X-MC-METADATA", "{\"OriginId\": \"" + Id + "\"}");
Check the API logs and see what the raw message looked like, it should have this content somewhere like:
X-MC-Metadata: { \"testkey\": \"testvalue\"}
What I am doing is storing all the metadata in a dictionary, using a JSON converter to create the string and then stripping out all /n /r characters. The end resulting string is set as the header value.

Using MailKit, can I get the subject or sender email without downloading entire message in POP?

I have an email with yahoo business and MailKit works with POP. I want to download the message after finding a specific subject. Or could I use IMAP?
If the POP3 server supports the TOP extension, you can download just the message headers to first check the subject. To do that, you could do something like this:
if (client.Capabilities.HasFlag (Pop3Capabilities.Top)) {
var headers = client.GetMessageHeaders (index);
if (headers[HeaderId.Subject] == subject)
message = client.GetMessage (index);
}
If your Yahoo account also supports IMAP, I would recommend using IMAP since IMAP allows you to query the server for messages with a given subject which is much more efficient than downloading the headers for every message to check if the subject matches the one you are looking for.

Example of creating a draft with an attachment in C#?

I've written a test application in C# that creates a draft message using the new Gmail API. It works fine when the message has no attachment.
I'm moving from the IMAP API and have used the MailBee.NET components with that API. The MailBee.NET components includes a class that produces an RFC 2822 message, so I've re-used this and have Base64-encoded the message and have assigned to the "Raw" property as described here:
https://developers.google.com/gmail/api/guides/drafts
MailMessage msg = new MailMessage();
msg.Subject = "test!";
msg.BodyPlainText = "Test content";
msg.Attachments.Add(#"D:\Trace.log", "Trace.log", Guid.NewGuid().ToString(), null, null, NewAttachmentOptions.Inline, MailTransferEncoding.Base64);
Message m = new Message();
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
Draft d = new Draft();
d.Message = m;
d = gs.Users.Drafts.Create(d, "me").Execute();
It works fine when no attachment is added, but fails with a 500 response when one is added:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "Backend Error"
}
],
"code": 500,
"message": "Backend Error"
}
}
Could somebody please provide an example of how to do this using the .NET API? The example on the API page is very barebones and doesn't really give much in the way of useful information and the documentation isn't great. It would probably be best to use the Message / MessagePart / MessagePartBody classes included with the .NET Client, however I can't find any clear guidance or examples on their use so don't know where to begin.
Questions:
1) Can anybody provide some example code of creating a draft message with an attachment using the classes within the .NET Client?
2) Is it possible to attach more than one file? The documentation refers to a single file throughout and the Multipart guidance refers to exactly two parts: metadata and attachment.
Providing a sample "raw" field that you're uploading would definitely be helpful to debug (either base64 encoded or just directly).
However this sounds related to:
GMail API : unable to add an attachment in a draft
also about this:
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
you want to make sure you're using "web safe" (aka "url safe") base64 encoding alphabet from https://www.rfc-editor.org/rfc/rfc4648#section-5
as it says in the docs at the URL you mentioned:
"""
Your application can create drafts using the drafts.create method. The general process is to:
Create a MIME message that complies with RFC 2822.
Convert the message to a URL-safe base64-encoded string.
Create a draft, setting the value of the drafts.message.raw field to the encoded string.
"""
Google APIs use the
Much like for the poster of the other question GmailGuy referred to, this has magically started working overnight. So it must've been a Gmail-side problem after all.
Regarding:
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
Thanks for the heads-up on this; I had actually encoded it previously but while trying 20 different things to get things working I removed it and forgot to add it back in!
Also, to confirm: yes, you're able to add more than one attachment when you use the raw message approach.

How can I validate OpenId token in C#?

I am trying to validate using this parameters:
"openid.mode=check_authentication"<br>
+ "&openid.assoc_handle=" + txtAssocHandle.Text<br>
+ "&openid.response_nonce=" + HttpUtility.UrlEncode(txtNonce.Text)<br>
+ "&openid.op_endpoint=" + txtEndpoint.Text<br>
+ "&openid.sig=" + txtSignature.Text<br>
+ "&openid.signed=mode,identity,return_to";
and it returns
is_valid:false
ns:http://specs.openid.net/auth/2.0
what am I doing wrong here?
the txt fields are being filled with login response values
Your openid.signed argument needs to be exactly what the OP sent to your RP rather than this incomplete hard-coded list of 3 parameters, for one thing. All your arguments should be URL encoded as well, not just your nonce.
There is a lot more to validating an OpenID token than just sending it back to the OP using "dumb mode". What are you trying to do?
Have you considered using an OpenID library? Seriously, getting OpenID right (meaning secure, and interoperable) is a big job. Way bigger than assembling just the right query string. :)

Categories

Resources