Read And Write To Header Message MQ C# - c#

I am reading an MQMessage in like so
queue.Get(message, gmo);
string message1 = message.ReadString(message.MessageLength);
I can see there is a message.UserId on MQMessage, but that is not enough
I want the users to get additional information in the message header of an MQMessage.
How can I set and then retrieve the following from a header of an MQMessage
username
password
mySpecialID

This can be done like this
msg.SetStringProperty("NicksProp", "blahblah");

Note: MQ treats message properties as plain text, so if you are sending a password as a message property then EVERYONE can see it. You will be able to see it via WireShark, and/or in the queue with any tool (MQ Explorer) and/or in the MQ log files.

Related

Access previous message content from a user reply message on Telegram

Does anyone know how can I access a previous message content when a user sends a reply message on Telegram?
for example:
James: hello
me: reply on James message with "hello friend"
here i want to get the hello message content or its message id in my telegram bot to process a command.
sth like this:
if(message.Contains("/delete")){
bot.DeleteMessageAsync(chatId, PreviousMessage or OriginalMessage ID)
}
PreviousMessage or OriginalMessage ID are equals to MessageId
but instead of MessageId i want to use sth like those two
I have checked telegram bot documentation and I did not find anything
How can i do it?
(Im using getUpdates method)
sorry for my poor English
You can find the original message ID in message.reply_to_message.message_id. Naming could differ depends on the library you use. More information and other attributes that could be useful you can find in the official Telegram's documentation for the Message object.

implementing "Reply this by email" for a ticket

I'm going to develop a ticketing system with c# that should send an email containing the ticket content to the receiver upon ticket submission and the receiver should be able to reply to that email which results in sender receiving the email of the reply. What puzzles me is that how am I going to keep track of that specific ticket which being replied by the receiver. I'm not looking for any code, just concepts or best practices.
Theoretically, you might use the Message-ID in conjunction with In-Reply-To, as described in the RFC 5322:
The "Message-ID:" field provides a unique message identifier that
refers to a particular version of a particular message. The
uniqueness of the message identifier is guaranteed by the host that
generates it (see below). This message identifier is intended to be
machine readable and not necessarily meaningful to humans. A message
identifier pertains to exactly one version of a particular message;
subsequent revisions to the message each receive new message
identifiers.
The "In-Reply-To:" and "References:" fields are used when
creating a reply to a message. They hold the message identifier of
the original message and the message identifiers of other messages
(for example, in the case of a reply to a message that was itself a
reply). The "In-Reply-To:" field may be used to identify the message
(or messages) to which the new message is a reply, while the
"References:" field may be used to identify a "thread" of
conversation.
When creating a reply to a message, the "In-Reply-To:" and
"References:" fields of the resultant message are constructed as
follows:
The "In-Reply-To:" field will contain the contents of the
"Message-ID:" field of the message to which this one is a reply (the
"parent message"). If there is more than one parent message, then the
"In-Reply-To:" field will contain the contents of all of the parents'
"Message-ID:" fields. If there is no "Message-ID:" field in any of the
parent messages, then the new message will have no "In-Reply-To:"
field.
Of course, you should keep tracking of mappings between the Message-ID field and you internal ticket number in a separate database table.
Example
A new email E1 is sent from yourCompany.com.
A reply R1 is received from yahoo.com. The message header information:
References:
<11111#yourCompany.com>
Message-ID:
<22222#webServer.yahoo.com>
In-Reply-To:
<11111#yourCompany.com>
A reply R2 to R1 is sent from yourCompany.com.
A reply R3 to R2 is received from yahoo.com. The message header information:
References:
<11111#yourCompany.com>
<22222#webServer.yahoo.com>
<33333#yourCompany.com>
Message-ID:
<44444#webServer.yahoo.com>
In-Reply-To:
<33333#yourCompany.com>
I think the only way to do this (and I never saw a ticketing-system who does that differently) is adding the ID into the subject-line.
In our case, we have subject-headers like "bla bla bla <<< CALLID: 12312 >>>".
With regex, that's quite easy to catch
Keep the ticket in the Subject in a specific format that the application can understand.
e.g. Subject can be Close TICKET T1 or
Reject Resolution TICKET T1 .
You can ask the customer to specify the reason in the mail body.
the trick is to give pre-formatted Subject which you understand.

C#: What is the best method to send support request via email?

I have a windows forms application that I am adding a request support form to, and would like the user to be able to input the values and hit a button. Once the button is pushed I can either:
Open a new mail message and auto populate the message. (Not sure how to do this)
Submit the request via a http form on my website. (I know how to do this)
Send an email directly from the code of the application. (I know how to do this)
What I want to know is what would be the best method to use? I think option 1 is the most transparent, and the user will see exactly what is being sent, but I am not sure how to ensure it works no matter what email client they use.
I see there being potential issues with option two, specifically a firewall possibly stopping the submission. But option 2 would allow me to supply them with a ticket number right then and there for their request.
Thanks for the help.
For Option 1, as suggested, use the mailto handler.
Format your string like so: string.Format("mailto:support#example.com?subject={0}&body={1}", subject, body). Don't forget to UrlEncode the subject and body values.
Then use System.Diagnostics.Process.Start() with your string.
This will launch the registered mail handler (Outlook, Windows Live Mail, Thunderbird, etc) on the system.
For option 1 : If the message body is short, then invoking the mailto handler from inside your code no longer requires that they be using outlook. It's kinda a cheap hack, but it's completely cross-platform for local mail clients. (If they're using something like gmail, you're still SOL, though)
Option 2) is the best to avoid enterprise firewall issues because the HTTP port may not be blocked.
Option 2) is the best for simple configuration. The only config key you will have is the service/page url. Then your SMTP configuration will stay on your webserver.
Now you will have to choose between using a webpage (if one already exists) or a webservice (which is best fitted for your feature).
For option (1) be prepared to deal with Outlook version problems. But this is not hard (again if we are talking about Outlook, last version)
//using Microsoft.Office.Interop.Outlook;
private void OutlookMail(string Subject, string Body)
{
ApplicationClass app = new ApplicationClass();
NameSpaceClass ns = (NameSpaceClass)app.GetNamespace("mapi");
ns.Logon("", "", true, true);
MailItem mi =
(MailItem)app.CreateItem(OlItemType.olMailItem);
mi.Subject = Subject;
int EOFPos = Body.IndexOf(char.Parse("\0"));
if (EOFPos != -1)
{
log.Error("EOF found in Mail body");
ErrorDialog ed = new ErrorDialog(TietoEnator.Common.ErrorDialog.ErrorDialog.Style.OK, "Export Error", "File could not be exported correctly, please inform responsible person", "", "EOF char detected in the body of email message.");
ed.ShowDialog();
Body=Body.Replace("\0", "");
}
mi.HTMLBody = "<html><head><META content='text/html; charset=CP1257' http-equiv=Content-Type></head><body><table>"+Body+"</table></body></html>";
mi.BodyFormat = OlBodyFormat.olFormatHTML;//.olFormatPlain;
mi.Display(0); // show it non - modally
ns.Logoff();
}
BTW for automatic support requests I plan to use in my current project "Microsoft Enterprise Logging Support Block" email sending functionality.

C# Send both HTML and Text email - most elegant?

Is it best practice to send both HTML and Text email?
If I only send HTML what are the dangers?
I'm thinking something like this below, from http://johnnycoder.com/blog/2009/04/15/net-mailmessage-linkedresources-alternateviews-and-exceptions/.
try
{
// Assign a sender, recipient and subject to new mail message
MailAddress sender =
new MailAddress("sender#johnnycoder.com", "Sender");
MailAddress recipient =
new MailAddress("recipient#johnnycoder.com", "Recipient");
MailMessage m = new MailMessage(sender, recipient);
m.Subject = "Test Message";
// Define the plain text alternate view and add to message
string plainTextBody =
"You must use an email client that supports HTML messages";
AlternateView plainTextView =
AlternateView.CreateAlternateViewFromString(
plainTextBody, null, MediaTypeNames.Text.Plain);
m.AlternateViews.Add(plainTextView);
// Define the html alternate view with embedded image and
// add to message. To reference images attached as linked
// resources from your HTML message body, use "cid:contentID"
// in the <img> tag...
string htmlBody =
"<html><body><h1>Picture</h1><br>" +
"<img src=\"cid:SampleImage\"></body></html>";
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(
htmlBody, null, MediaTypeNames.Text.Html);
// ...and then define the actual LinkedResource matching the
// ContentID property as found in the image tag. In this case,
// the HTML message includes the tag
// <img src=\"cid:SampleImage\"> and the following
// LinkedResource.ContentId is set to "SampleImage"
LinkedResource sampleImage =
new LinkedResource("sample.jpg",
MediaTypeNames.Image.Jpeg);
sampleImage.ContentId = "SampleImage";
htmlView.LinkedResources.Add(sampleImage);
m.AlternateViews.Add(htmlView);
// Finally, configure smtp or alternatively use the
// system.net mailSettings
SmtpClient smtp = new SmtpClient
{
Host = "smtp.example.com",
UseDefaultCredentials = false,
Credentials =
new NetworkCredential("username", "password")
};
//<system.net>
// <mailSettings>
// <smtp deliveryMethod="Network">
// <network host="smtp.example.com"
// port="25" defaultCredentials="true"/>
// </smtp>
// </mailSettings>
//</system.net>
smtp.Send(m);
}
catch (ArgumentException)
{
throw new
ArgumentException("Undefined sender and/or recipient.");
}
catch (FormatException)
{
throw new
FormatException("Invalid sender and/or recipient.");
}
catch (InvalidOperationException)
{
throw new
InvalidOperationException("Undefined SMTP server.");
}
catch (SmtpFailedRecipientException)
{
throw new SmtpFailedRecipientException(
"The mail server says that there is no mailbox for recipient");
}
catch (SmtpException ex)
{
// Invalid hostnames result in a WebException InnerException that
// provides a more descriptive error, so get the base exception
Exception inner = ex.GetBaseException();
throw new SmtpException("Could not send message: " + inner.Message);
}
I would say that, in today's world, the "best-practice" approach would be to ensure that you send your message as both plain text and HTML (if you really want to send HTML email messages).
Oh, and make sure you do actually send the content in the plain text view, rather than a single sentence saying "You must use an email client that supports HTML messages". Google Mail takes this approach, and it seems to work perfectly, allowing "rich" views on full-fledged PC clients, whilst also allowing "minimal" views on more restricted devices (i.e. Mobile/Cell phones).
If you want to take a purist's view, you wouldn't be sending HTML emails at all, nor would you ever "attach" a binary file to an email. Both corruptions of the original email standard, which was only ever originally intended for plain text.
(See some people's opinions of this here and here)
However, in the pragmatic modern-day real world, HTML email is very real, and very acceptable. The primary downside to sending HTML email is whether the recipient will see the email in the way that you intended them to see it. This is much the same problem that web designers have battled with for years; getting their websites to look "just right" in all possible browsers (although it's significantly easier today than it was many years ago).
Similar to ensuring that a website functions correctly without requiring Javascript, by sending your emails as both HTML and Plain Text, you'll ensure that your emails degrade gracefully so that people reading their emails on (for example) small mobile devices (something that's becoming more and more prevalent these days - and which may or may not be capable of rendering a complete HTML email) can still read your email content without issue.
If you only send HTML, then anyone reading email on a text-only device will have trouble.
For example, I suspect many low-end mobile devices are capable of reading email but not displaying full HTML.
I would say it's best practice to either send text-only, or text and HTML.
I don't see why you're catching a bunch of exceptions only to rethrow the same exception type with a different message, by the way - the original message may well be more descriptive.
Another reason to send both is that many mailservers mark emails that only contain HTML content as spam. You don't want all your emails to be put in the junk folder.
I think yes, the best practice is to send both. The reason (c&p from wikipedia):
The default e-mail format according to RFC 2822 is plain text. Thus
e-mail software isn't required to support HTML formatting. Sending
HTML formatted e-mails can therefore lead to problems at the
recipient's end if it's one of those clients that don't support it. In
the worst case, the recipient will see the HTML code instead of the
intended message.
Sharing my experience with sending both HTML and text in one email:
I have created an email message that has 2 views: text and html using C# AlternateView classes.
What did I get?
On Mac, tested on High Sierra:
Apple Mail app was showing the Html. If the order of messages is reversed: Html - text then Apple Mail will show the text view. The conclusion: Apple Mail is using the last view as default.
In Windows, Outlook 2010:
Microsoft Outlook by default is using the Html view. The order of views in the email doesn't matter: html,text; text,html;
If for some reason you selected a setting to show incoming messages as a text then the Html version of your email will be converted into the text by Outlook.
Even so you send the text version of your email (which might be slightly different from the HTML version and was formatted to look pretty) it won't be used.
So you don't need to send the text version of your email if you know that your clients use Outlook and Html version is selected as default.
Mozilla Thunderbird respects your settings and shows the correct Html or text version of your email. It works correctly on Mac and in Windows
Hope it helps
Several email clients will use the last AlternateView that was added to the AlternateViews.
So if you prefer to have your mail displayed as HTML, be sure to add that last.
I have notice this for IOS mail and OSX mail, while Android Email seems to prefer HTML if it is available. I am not sure for which versions this holds, and the behaviour is often user-configurable, but in my experience these were the defaults.

How to Domainkeys/DKIM email signing using the C# SMTP client?

I have written an program in C# which sends out emails. Now I have a requirement to sign outbound emails using Dominkeys/DKIM, but I'm not sure how to do it.
I have set up all keys, but I don't know how to get those and how to include them in the email header.
There is a fundamental problem with trying to do DKIM signatures with System.Net.Mail.MailMessage and System.Net.Mail.SmtpClient which is that in order to sign the message, you need to poke the internals of SmtpClient in order to hash the message body as one of the steps in generating the DKIM-Signature header. The problem comes in when you have alternative views or attachments because SmtpClient will generate new multipart boundaries each time it writes out the message which breaks the body hash and thus the DKIM-Signature validity.
To work around this, you can use the MimeKit and MailKit open source libraries for .NET as an alternative framework to using System.Net.Mail.
To add a DKIM signature to a message in MimeKit, you would do something like this:
MimeMessage message = MimeMessage.CreateFromMailMessage(mailMessage);
HeaderId[] headersToSign = new HeaderId[] { HeaderId.From, HeaderId.Subject, HeaderId.Date };
string domain = "example.net";
string selector = "brisbane";
DkimSigner signer = new DkimSigner ("C:\my-dkim-key.pem", domain, selector)
{
SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1,
AgentOrUserIdentifier = "#eng.example.com",
QueryMethod = "dns/txt",
};
// Prepare the message body to be sent over a 7bit transport (such as
// older versions of SMTP). This is VERY important because the message
// cannot be modified once we DKIM-sign our message!
//
// Note: If the SMTP server you will be sending the message over
// supports the 8BITMIME extension, then you can use
// `EncodingConstraint.EightBit` instead.
message.Prepare (EncodingConstraint.SevenBit);
message.Sign (signer, headersToSign,
DkimCanonicalizationAlgorithm.Relaxed,
DkimCanonicalizationAlgorithm.Simple);
To send the message using MailKit, you would do something like this:
using (var client = new MailKit.Net.Smtp.SmtpClient ()) {
client.Connect ("smtp.gmail.com", 465, true);
client.Authenticate ("username", "password");
client.Send (message);
client.Disconnect (true);
}
Hope that helps.
see https://github.com/dmcgiv/DKIM.Net it's a DomainKeys Identified Mail (DKIM) implementation for .Net written in C# - it enables you to sign MailMessage objects.
Use
http://www.mimekit.org
Not only does it allow to use DKIM for signing, also you can include S/MIME certificates, PGP certificates and more.
Also, its a very mature lib - the only one i've found that handles foreign languages (apart from english) correctly, since its completely and thoroughly coded with unicode in mind.
Its free and opensource.
This solved it for me when using Mailenable as SMTP relay server.
http://www.mailenable.com/kb/content/article.asp?ID=ME020700
When creating the DKIM TXT record on the domain name don't forget to use the active selector as prefix => yourselector._domainkey.yourdomainname.be
If you are looking to DKIM-sign the body of the MailMessage then DKIM.NET is great. If you are looking to have alternative views in your message then I wasnt able to find a solution and wrote my own (open-source with the usual disclaimers) that can be found at https://github.com/yannispsarras/DKIM-AlternativeViews
I understand this is a pretty old thread but I thought it may help someone.
i didnt find much help on this issue, but my problem got solve by configuring smtp server.
i cant post those steps as i am using 3rd party smtp server and every server has their own configuration. after proper configuration my smtp automatically adds DM/DKIM signature.

Categories