Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

send email (c#) using ews and set custom display name (from)

$
0
0

How to connect to exchange service via exchange web services send a mail and the display name is custom ?
With this code, just connect to exchange server (ews) and send a mail.
How to change my code ?

        private void t() {
            const string subjectBody = "test email ";
            const string username = "username";
            const string password = "password";
            const string domain = "domain";
            const string ewsURL = "http://exchangesrv/ews/exchange.asmx";

            unews.ExchangeServiceBinding esb = new unews.ExchangeServiceBinding();
            esb.Credentials = new System.Net.NetworkCredential( username, password, domain );

            esb.Url = ewsURL;
            unews.CreateItemType newItem = new unews.CreateItemType();
            newItem.MessageDisposition = unews.MessageDispositionType.SendAndSaveCopy;
            newItem.MessageDispositionSpecified = true;

            unews.MessageType msg = new unews.MessageType();

            msg.Subject = subjectBody;
            msg.Body = new unews.BodyType();
            msg.Body.BodyType1 = unews.BodyTypeType.Text;
            msg.Body.Value = subjectBody;
            msg.ToRecipients = new unews.EmailAddressType[1];
            msg.ToRecipients[0] = new unews.EmailAddressType();
            msg.ToRecipients[0].EmailAddress = "mail@domain.com";
            newItem.Items = new unews.NonEmptyArrayOfAllItemsType();
            newItem.Items.Items = new unews.ItemType[1];
            newItem.Items.Items[0] = msg;

            try {
                unews.CreateItemResponseType createItemResponse = esb.CreateItem( newItem );
                if (createItemResponse.ResponseMessages.Items[0].ResponseClass == unews.ResponseClassType.Error) {
                    throw new Exception( createItemResponse.ResponseMessages.Items[0].MessageText );
                }
                else {
                    Console.WriteLine( "Item was created" );
                }
            }
            catch (Exception ex) {
                Console.WriteLine( ex.ToString() );
            }
        }


/*
With this code, can connect to a exchange server (smtp) and send a mail, with a custom display name.

System.Net.Mail.SmtpClient smtpc = new System.Net.Mail.SmtpClient( "exchangesrv"  );
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage( "CustomDisplayName <email@domain.biz>", "email@domain.biz", "test email", "test email" );
smtpc.Credentials = new System.Net.NetworkCredential( "username", "password", "domain" );
smtpc.Send( mm );

*/


Viewing all articles
Browse latest Browse all 7132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>