Reputation: 309
I have written a very basic class to send an email. I tested that with smtp server and it works fine, but when I try to use my company's exchange server it giving this exception:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
My code is as below:
MailMessage mailMessage = new MailMessage("[email protected]", "[email protected]", "Test Subject", "Void body");
SmtpClient smtpClient = new SmtpClient(smtpServerAddress, smtpServerPort);
NetworkCredential credentials = new NetworkCredential(AccountName,Password);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; // without this I get: The remote certificate is invalid according to the validation procedure.
smtpClient.Send(mailMessage);
Do I need to handle exchange server differently than smtp server.
Please advice Thanks
Upvotes: 2
Views: 6348
Reputation: 136
I just had this problem. On your exchange server enter the Exchange System Manager and open the object tree to the server, then protocols, and right click on default SMTP virtual server and select properties. Click the access tab and then the authentication button. Finally click users and make sure that the user that you are using has access to the SMTP Relay. In addition, if you have not done so already select the relay button under the SMTP and make sure that the machine running the application is not blacklisted, or has been granted permission.
Upvotes: 1
Reputation: 2560
You may want to try sending using WebDAV. Here's my method if you want to try it out..
public static void ViaWebDav(String sMailbox, String sExchange, String sTo, String sCc, String sSubject, String sBody, params String[] sAttachments)
{
HttpWebRequest hwrOut;
HttpWebResponse hwrIn;
//String strServer = "SXGM-202.xxx.com";
//string strPassword = "123";
//string strUserID = "u";
//string strDomain = "fg";
Byte[] b = null;
Stream s = null;
String sMailboxUrl = "http://" + sExchange + "/exchange/" + sMailbox;
String sMailboxSend = sMailboxUrl + "/##DavMailSubmissionURI##/";
String sMailboxTemp = sMailboxUrl + "/drafts/" + sSubject + ".eml";
// Construct the RFC 822 formatted body of the PUT request.
// Note: If the From: header is included here,
// the MOVE method request will return a
// 403 (Forbidden) status. The From address will
// be generated by the Exchange server.
StringBuilder sb = new StringBuilder();
sb.AppendLine("To: " + sTo);
if (!sCc.IsEmpty()) sb.AppendLine("Cc: " + sCc);
sb.AppendLine("Subject: " + sSubject);
sb.AppendLine("Date: " + System.DateTime.Now);
sb.AppendLine("X-Mailer: AML FIU;");
sb.AppendLine("MIME-Version: 1.0;");
sb.AppendLine("Content-Type: text/plain;");
sb.AppendLine("Charset = \"iso-8859-1\"");
sb.AppendLine("Content-Transfer-Encoding: 7bit;");
sb.AppendLine();
sb.AppendLine(sBody);
// Create a new CredentialCache object and fill it with the network
// credentials required to access the server.
//MyCredentialCache = new CredentialCache();
//MyCredentialCache.Add(new System.Uri(strMailboxURI),
// "NTLM",
// new NetworkCredential(strUserID, strPassword, strDomain)
// );
// Create the HttpWebRequest object.
hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
hwrOut.Credentials = CredentialCache.DefaultCredentials;
hwrOut.Method = "PUT";
hwrOut.ContentType = "message/rfc822";
// Encode the body using UTF-8.
b = Encoding.UTF8.GetBytes(sb.ToString());
hwrOut.ContentLength = b.Length;
s = hwrOut.GetRequestStream();
s.Write(b, 0, b.Length);
s.Close();
// PUT the message in the Drafts folder of the mailbox.
hwrIn = (HttpWebResponse)hwrOut.GetResponse();
#region //ATTACHMENTS
//Do the PROPPATCH
sb = new StringBuilder();
sb.Append("<?xml version='1.0'?>");
sb.Append("<d:propertyupdate xmlns:d='DAV:'>");
sb.Append("<d:set>");
sb.Append("<d:prop>");
sb.Append("<isCollection xmlns='DAV:'>False</isCollection>");
sb.Append("</d:prop>");
sb.Append("</d:set>");
sb.Append("</d:propertyupdate>");
foreach (String sAttach in sAttachments)
{
hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
hwrOut.Credentials = CredentialCache.DefaultCredentials;
hwrOut.Method = "PROPPATCH";
hwrOut.ContentType = "text/xml";
hwrOut.Headers.Set("Translate", "f");
b = Encoding.UTF8.GetBytes(sb.ToString());
hwrOut.ContentLength = b.Length;
s = hwrOut.GetRequestStream();
s.Write(b, 0, b.Length);
s.Close();
hwrIn = (HttpWebResponse)hwrOut.GetResponse();
hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp + "/" + Path.GetFileName(sAttach));
hwrOut.Credentials = CredentialCache.DefaultCredentials;
hwrOut.Method = "PUT";
using (FileStream fs = new FileStream(sAttach, FileMode.Open, FileAccess.Read))
{
b = new Byte[fs.Length];
fs.Read(b, 0, (Int32)fs.Length);
}
hwrOut.ContentLength = b.Length;
s = hwrOut.GetRequestStream();
s.Write(b, 0, b.Length);
s.Close();
hwrIn = (HttpWebResponse)hwrOut.GetResponse();
}
#endregion
// Create the HttpWebRequest object.
hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
hwrOut.Credentials = CredentialCache.DefaultCredentials;
hwrOut.Method = "MOVE";
hwrOut.Headers.Add("Destination", sMailboxSend);
hwrIn = (HttpWebResponse)hwrOut.GetResponse();
// Clean up.
hwrIn.Close();
}
Upvotes: 0
Reputation: 854
I found this post about using exchange to send the email. It appears that he is using the Microsoft.Exchange.WebServices namespace instead. I don't have exchange to test with. Just putting out ideas.
http://waqarsherbhatti.posterous.com/sending-email-using-exchange-server-2010-ews
Upvotes: 0
Reputation: 23123
Are you using the right port number?
Protocol: SMTP/SSL
•Port (TCP/UDP): 465 (TCP)
•Description: SMTP over SSL. TCP port 465 is reserved by common industry practice for secure SMTP communication using the SSL protocol. However, unlike IMAP4, POP3, NNTP, and HTTP, SMTP in Exchange 2000 does not use a separate port for secure communication (SSL), but rather, employs an "in-band security sub-system" called Transport Layer Security (TLS). To enable TLS to work on Exchange 2000, you must install a Computer certificate on the Exchange 2000 server.
Ripped from here: http://www.petri.co.il/ports_used_by_exchange.htm
Upvotes: 3