Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, December 10, 2010 7:33 PM
Hi,
I am trying to send email direct using C#. Earlier I tried to send mail from C# using outlook , but it seems it can be made as a completely automated process because of security prompts from MS Outlook.
So for sending mails directly through C#, I thought of looking into the configuration of my already configured outlook. I see that its configured to use Microsoft exchange server and I get the server address also.
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("exchangebox1.mycompany.com");
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage("[email protected]",
"[email protected]",
"title here",
"body here");
client.Send(msg);
But I get an exception precisely when at client.send(msg).
Am I missing a point here ?
Thanks
AJ
All replies (5)
Thursday, December 16, 2010 7:06 AM âś…Answered | 1 vote
Hello Ajay Nair,
Kindly, find the code below which will help you:
How to Send a Message
Once you create a message, you need to send it through an SMTP (Simple Message Transfer Protocol) server, which in turn will forward it to the recipient. In the .NET Framework, the SmtpClient class represents the SMTP server. Most of the time, sending a message is as simple as this sample code (where "smtp.contoso.com" is the name of the local SMTP server):
' VB
Dim m As MailMessage = New MailMessage _
("[email protected]", _
"[email protected]", _
"Quarterly data report.", _
"See the attached spreadsheet.")
Dim client As SmtpClient = New SmtpClient("smtp.contoso.com")
client.Send(m)
// C#
MailMessage m = new MailMessage
("[email protected]",
"[email protected]",
"Quarterly data report.",
"See the attached spreadsheet.");
SmtpClient client = new SmtpClient("smtp.contoso.com");
client.Send(m);
How to Configure Credentials
To reduce spam, all SMTP servers should reject messages from unauthorized users when the message recipients are not hosted by the SMTP server. SMTP servers provided by ISPs typically determine whether a user is authorized based on the user's IP address; if you are part of the ISP's network, you are allowed to use the SMTP server.
Other SMTP servers (and some ISP SMTP servers) require users to provide a valid username and password. To use the default network credentials, set SmtpClient.UseDefaultCredentials to True. Alternatively, you can set SmtpClient.Credentials to CredentialCache.DefaultNetworkCredentials (in the System.Net namespace), as the following code demonstrates:
' VB
Dim client As SmtpClient = New SmtpClient("smtp.contoso.com")
client.Credentials = CredentialCache.DefaultNetworkCredentials
// C#
SmtpClient client = new SmtpClient("smtp.contoso.com");
client.Credentials = CredentialCache.DefaultNetworkCredentials;
To specify the username and password, create an instance of the System.Net.NetworkCredential class and use it to define SmtpClient.Credentials. The following example shows hard-coded credentials; however, you should always prompt the user for credentials:
' VB
Dim client As SmtpClient = New SmtpClient("smtp.contoso.com")
client.Credentials = New NetworkCredential("user", "password")
// C#
SmtpClient client = new SmtpClient("smtp.contoso.com");
client.Credentials = new NetworkCredential("user", "password");
You might need to set the port no, kindly find the link below for the same:
Thanks,
Paras Sanghani
http://parassanghani.blogpsot/
Mark As Answer if it helped you.
Friday, December 10, 2010 7:43 PM
what exception are you getting? You may have to set the credentials of the smtp client.
Saturday, December 11, 2010 3:46 AM
The exception i get is "Sending has failed" thats it. How do I ensure that I set up proper credentials ? I did .UseDefaultCredentials = true but that dint help
I checked the Inner Exception and it says "Unable to connect to the remote server". Does that mean the exchange server address I have given is incorrect. As I said I have a completely functionl and configured outlook express. Can you tell from where can I obtain the exchange server address.
Currently I got it from Tools -> Email Accounts -> View or change existing Email.
And there I get the exchange server address.
Thanks
AJ
Sunday, December 12, 2010 9:27 AM
Any ideas anyone ? ?
Thursday, December 16, 2010 5:55 AM
Hi Ajay,
Thanks for your feedback.
I think this link may help you, Please refer here: http://social.msdn.microsoft.com/forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8/
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.