How to send an email in EWS/C# with Suppress Auto Responses? - c#

I've been trying to find an answer to this within MSDN documentation and various other resources, but am unable to find something that works.
Here is some C# code I am using:
private ExtendedPropertyDefinition SurpressAutoResponse = new ExtendedPropertyDefinition(
DefaultExtendedPropertySet.InternetHeaders,
"X-Auto-Response-Suppress",
MapiPropertyType.String); // Also tried with StringArray and Integer
private ExtendedPropertyDefinition OtherID = new ExtendedPropertyDefinition(
DefaultExtendedPropertySet.InternetHeaders,
"X-Custom-ID-Property-Example",
MapiPropertyType.String);
{ some other code that's unimportant in between }
var mm = new EmailMessage(Global.Exchange);
mm.ToRecipients.Add("me#me.com"); // example address, of course
mm.Subject = Subject.Replace('\r', ' ').Replace('\n', ' ');
mm.SetExtendedProperty(SurpressAutoResponse, "OOF, NDR"); // Also tried {"OOF", "NDR"} and -1
mm.SetExtendedProperty(OtherId, "12345-1");
mm.Body = "Hello World";
mm.Send();
When I inspect the headers for the incoming email, I see that my "OtherId" is correctly set, but the X-Auto-Response-Suppress is not set. Any ideas how I should be getting exchange to suppress these out of office and delivery failure reports?
Notes:
I am targeting an Exchange 2010_SP2 server, which should support this
References:
https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcmail/ced68690-498a-4567-9d14-5c01f974d8b1
https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd633654(v=exchg.80)
UPDATE
I decided to try testing the behavior of the email and set an auto-reply/OOF on my email account. Even though the properties of the email do not include the X-Auto-Response-Suppress header, I noticed that it indeed prevented a reply. My presumption is that the header is read on the Exchange server, which also probably processes the auto-responses instead of the client. Since the client doesn't act upon the header itself, Exchange saves some data by removing it from the original email before it's transferred. Can anyone who knows please confirm this is correct?

I have had issues using that header before as the MSDN is very vague on what all it actually does. And it is mostly only utilized by exchange servers and OOF purposes. Instead there are other headers that work better. Here is an article explaining why I think it would serve you well to use other headers. https://www.jitbit.com/maxblog/18-detecting-outlook-autoreplyout-of-office-emails-and-x-auto-response-suppress-header/
If you are only wanting to catch OOF then you can change the header to:
X-Auto-Response-Suppress:OOF
But I don't see that as a good example. Here is another thread on why this isn't always the best header to use: Detecting Outlook autoreply/out-of-office emails

Related

C# - Sending Email From Within SHarepoint

I would like to send an email from within sharepoint to a user on the local domain, after an event reciever has been triggered.
How would I go about this?
Any help would be much appreciated.
Many Thanks,
Freddie
You didn't really gave much context to your question, so I'm just going to point out the simplest alternative.
In most situations, you should be just be able to use one of the SPUtility.SendMail(..) overloads
So, something like this should be sufficient
var headers = new StringDictionary();
headers.Add("from", from);
headers.Add("to", to);
headers.Add("cc", cc);
headers.Add("bcc", bcc);
headers.Add("subject", subject);
headers.Add("content-type", "text/html");
SPUtility.SendEmail(web, headers, body);
That said, keep in mind that SPUtility.SendEmail isn't very robust (sometimes, it won't even point out if an error occurred while sending the mail...).
For this reason, some sources prefer to just use the well documented SmtpClient instead. In that case, the only problematic part is getting the outbound email server address.
SmtpClient client = new SmtpClient();
client.Host = currentWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
As you can see, one possible option is getting it from the web application associated to the current web site (that is, assuming you have a valid SPContext at the time and are therefore able to access the current web site in the first place). From here, just build the MailMessage instance and send it using client.Send(message);.

Internet Message ID FROM EWS Managed API Send Email c#

I am trying to discover if there is a way to determine the internet message ID after sending an email using the EWS Managed API. I understand you can go in there and get the results from the sent box, but the server that is sending these emails is sending multiple emails at a time from different services.
No you can't, basically because EWS sends message Asynchronously the Id isn't available see https://social.msdn.microsoft.com/Forums/azure/en-US/dd034b8c-ffa1-4ae0-9025-45fcf520c9e5/updateitem-does-not-return-itemid?forum=exchangesvrdevelopment
As a work around you might want to consider setting the Internet messageId on the Message before you send it. As long as it valid and unique it should work okay eg
ExtendedPropertyDefinition PidTagInternetMessageId = new ExtendedPropertyDefinition(4149, MapiPropertyType.String);
EmailMessage ema = new EmailMessage(service);
ema.Subject ="test from ews";
ema.Body = new MessageBody("test<br>Rgds<>");
ema.ToRecipients.Add("gscales#domain.com");
ema.SetExtendedProperty(PidTagInternetMessageId,("<" +Guid.NewGuid().ToString() + "#domain.com>"));
ema.SendAndSaveCopy();
Also if you save the message first as a draft before sending it the server will assign the MessageId property which which should then be able to read back using Load.
Cheers
Glen

Exchange Managed API determine if email has been anwered - PR_LAST_VERB_EXECUTED

Is there a way to determine if a given email in the inbox has been answered (Reply to Sender)
Actually I am using the following code, which does not work correct
ExtendedPropertyDefinition pr_last_verb_executed = new ExtendedPropertyDefinition(0x1081, MapiPropertyType.Integer);
PropertySet psPropSet = new PropertySet(pr_last_verb_executed);
ItemId itemId = new ItemId("<PLACEHOLDER FOR AN Exchange EMail ID>");
EmailMessage serverMail = EmailMessage.Bind(exchangeService, itemId, psPropSet);
int lastState = 0; // should become 102 if email has been answered
serverMail.TryGetProperty(pr_last_verb_executed, out lastState); // does not return anything to variable lastState
As I found out updating Exchange Server to a newer version solves this problem. So this ia a bug in older versions of Microsoft Exchange Server.
The best thing to do would be to look at the messages in question with a MAPI editor like MFCMapi or OutlookSpy that will tell you if that property has been set or not (its an optional property set by the client). The property is just an indicator of the last action a client took https://msdn.microsoft.com/en-us/library/ee218425(v=exchg.80).aspx reply is one action but there are many others. PidTagIconIndex https://msdn.microsoft.com/en-us/library/office/cc815472.aspx is another indicator property you can use to do the same thing. EWS also have conversation operations https://msdn.microsoft.com/en-us/library/office/dn610351(v=exchg.150).aspx if you are looking to work with conversation.
Cheers
Glen

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 do I hide email information in a .net application

I am creating a free program that has a support request page. This will send me an email and a log file so I can see what is going on.
I have created the email class, but like any email program/class it will require a username, password, email address, etc.
Now, once I put this out, I know that, with the right programs, you can view the code behind on .Net, which I really thing is completely absurd.
Anyhow, I don't want this information out there because it's a support email address and is sensitive information.
What is the best way for me to send an email but not include my sensitive information for all the nosy people out there or hide it so they cannot get to it?
Here is an example of the code I'm referring to:
var emailSettings = new EmailSettings();
emailSettings.Body = richTextBox_Message.Text;
emailSettings.BodyIsHtml = false;
emailSettings.EmailServerEnableSsl = true;
emailSettings.EmailServerPassword = "";
emailSettings.EmailServerPort = 25;
emailSettings.EmailServerUsername = "";
emailSettings.EmailSmtpServer = "";
emailSettings.FromEmailAddress = new MailAddress(textBox_EmailAddress.Text);
emailSettings.Subject = comboBox_TypeOfRequest.Text;
Scary stuff in there :D
Just to clarify, this is a free app so I cannot afford a program to hide the code. :(
It's not safe to use this method to send error reports. Consider using a .php file on a web server to send yourself errors, or some other method.
PHP Email Tutorial
But if you really want to, I believe you might be able to use System.Security.Cryptography to protect your data, I'm not 100% sure on that though, so correct me if I'm wrong.
http://msdn.microsoft.com/en-us/library/system.security.cryptography(v=vs.110).aspx

Categories

Resources