C# Mail - Separate Sender and Return Path - c#

I am attempting to set a different return path and sender in the headers to replicate a previous PHP system. It needs to be exactly the same.
When I put in a Sender it sets this to the return path and I can't seem to find a solution to separate these. Also the sender shows as behalf of in the From part of the mail message which I do not want.
Any recommendations?

Use:
MailMessage mail = new MailMessage("sender#email.com", "to#email.com");
mail.ReplyToList.Add(new MailAddress("reply#email.com", "reply-to"));
Please note MailMessage.ReplyTo is now obsolete.
Documentation

Have a look at the MailMessage.ReplyTo property.
Use the ReplyTo property to indicate an address other than the From address to use to reply to this message.

I used the MailKit library to solve my issue.
This allows you to specify a different sender at server level

Related

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

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

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);.

Reply/ReplyAll to a particular Outlook mail using C#

I want to send a reply to say a particular mail from my Outlook mailBox.
I intend to find the mail by searching the mailbox using the subject/body/sender and then reply to the found mail.
I have succeeded in searching the mail from the mailbox using C#, but i'm not able to reply to the mail.
And also if i use the reply will it be the exact replica of the action performed in outlook i.e. if replied on a mail, will the subject be added with RE: or we need to manually append the text to subject?
Please spare my Ignorance
Any help will be appreciated
The following code is an extract
Lets assume you've picked your item, here I picked one by a number..
MailItem m = objFolder.Items[t];
m.ReplyAll();
This effectively hits "ReplyAll" and fills all the things in as if outlook did it..(because it did) eg, add stuff to the body.. hit send.
Goutham gauti its right. All changes that you make in your mail item will be response. But if you want more information you can read this article on CodeProject:
http://www.codeproject.com/Articles/1106804/Create-and-send-an-email-reply-in-Csharp-VB-NET
private void ReplyToMail(Outlook.MailItem mailItem)
{
//mailItem is the mail you wand to reply to
Outlook.MailItem replyMail = mailItem.Reply();
//you can use replyAll insted
replyMail.Body = "the mail body text";
((Outlook._MailItem)replyMail).Send();
}

Exchange Web Services - Organizer Property

I am using Exchange Web Services trying to find the Organizer of the meeting's email address
I have tried using
Appoint.Organizer.Address
but some of the properties are null (see image).
How do I get the email address of the organizer?
Link to image (sorry not enough rep to embed)
http://i.stack.imgur.com/wSv2r.png
What operation are you using ? If you have just used FindItems then that's what you would expected because only the displayName of the Sender (which is the Organizer) is returned with FindItems. To get the Address property populated you would need to do a GetItem (or Load in the Managed API).Or if you really want to save a call you could try using the PidTagSenderSmtpAddress extended property http://msdn.microsoft.com/en-us/library/office/jj713594(v=office.15).aspx
Cheers
Glen
Do you know how this meeting came into the mailbox? Was it sent from a sender outside of Exchange, or another mailbox in that Exchange organization? What version of Exchange? Also how are you binding to the appointment? It would be good to see that code. I've tried this with a few meetings here and they all have the Address property populated. Your screenshot shows a MailboxType field of "OneOff", and I'm not sure off the top of my head how to make that happen.
Typically "OneOff" refers to a recipient that couldn't be resolved. In this case, you might try taking the information that is present (in this case the display name) and calling ResolveName to see if you can get the address that way.

Implementing a mailing list in .NET

I am implementing a mailing list using using .NET. As discussed in this answer, I need to send an email where the recipient of the envelope is different from the recipient in the To header. How to achieve this in C#? The SmtpClient and MailMessage classes in System.Net.Mail doens't seem to permit this.
I tried:
message.To.Add("list#example.com");
message.Headers["Envelope-to"] = "user#example.com";
but the mail doesn't get sent to what it is specified in the Envelope-to.
Any suggestions?
Adding an address to Envelope-To without adding it to To
You can use the MailMessage.Bcc property. Addresses added there will only appear in the Envelope-To, not in the mail's To:
message.Bcc.Add("user#example.com");
Adding an address to To without adding it to Envelope-To
Here, I'm quite sure you are out of luck. I've had a look at the System.Net.Mail namespace with ILSpy, and it looks like this is not possible. The To header of the mail is created out of the To property of the MailMessage (see Message.PrepareHeaders), and the same property is used to fill the Envelope-To of the mail (together with the Cc and Bcc properties, see SmtpClient.Send). Manually setting Headers["To"] won't help, since this value is overwritten with the contents of the To property (see Message.PrepareHeaders).
So, list#example.com will get a copy of the message. Depending on the configuration of your SMTP server, this might lead to a mail loop.

Categories

Resources