1

I have been roaming forums and not finding any answer to my question. all of the solution (and question) is about using Microsoft.Office.Interop.Outlook; for some reason I am not allowed to use any office.interop.

I even tried:

MailAddress fromAddress = new MailAddress("sender@server.com");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = fromAddress;
message.To.Add("receiver@theirServer.com");
message.CC.Add("receiver2@theirServer.com");
message.Subject = "theSubject";
message.Body = "TheBody";

SmtpClient smtpClient = new SmtpClient("zzz.server.xxx");
smtpClient.Credentials = new NetworkCredential("sender@server.com", "password");

smtpClient.Send(message);

the code fail to authenticate the credential, even after I hard-coded the password, but I feel that there must be a better way.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
The Pig
  • 11
  • 2

3 Answers3

2

There is a lot to be desired of your question. Can you post the response from the server or the error you are receiving?

Here are some observations and feedback that may help you Specify the port

SmtpClient smtpClient = new SmtpClient("zzz.server.xxx", PORTNUMBER);

Set some of the basic properties

smtpClient.Timeout = 10000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false; //must be set prior to credentials
smtpClient.Credentials = new NetworkCredential("username", "pass");

Set the message encoding

message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;

Send Asyncronously and use a callback to determine the result

//add callback 
smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

//set token
string userToken = "tokenString";

//send asynchronously
smtpCient.SendAsync(message, userToken);

public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
             String token = (string) e.UserState;

            if (e.Cancelled)
            {
                 //do something if it was cancelled
            }
            if (e.Error != null)
            {
               MessageBox.Show( e.Error.ToString());
            } else
            {
                MessageBox.Show("Message sent.");
            }
        }
Benjiman
  • 420
  • 2
  • 9
  • Just to add, you may need to Enable SSL using the EnableSsl property on the SmtpClient. [SmtpClient Class](http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx) – Kevin Anderson Mar 03 '13 at 05:21
0

Have you verified the credentials, including the format of the username through configuring the account in an alternative client, such as Outlook?

You might need:

smtpClient.Credentials = new NetworkCredential("sender", "password");

or

smtpClient.Credentials = new NetworkCredential("DOMAIN\sender", "password");
Daniel Holder
  • 111
  • 1
  • 5
0

Is your email server an Exchange Server? If so, you can use the Exchange Web Services (EWS) to send emails and save copies in the Sent Items folder. A simple example can be seen here:

Send Exchange Email

More Sample Code

Kevin Anderson
  • 589
  • 4
  • 17