Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public void SendEmail(string message)
{
MailMessage mesg = new MailMessage();
MailAddress sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
MailAddress receiver = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
SmtpClient smtpc = new SmtpClient()
{
Host = ConfigurationManager.AppSettings["smtpServer"],
Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
EnableSsl = true,
Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]),
DeliveryMethod = SmtpDeliveryMethod.Network
};
mesg.From = sender;
mesg.To.Add(receiver);
mesg.Body = message;
smtpc.Send(mesg);
smtpc.Dispose();
}

Here is my app.config code:

XML
<appSettings>
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="smtpServer" value="smtp.gmail.com" />
    <add key="EnableSsl" value = "true"/>
    <add key="smtpPort" value="25" />
    <add key="smtpUser" value="somemailid@gmail.com" />
    <add key="smtpPass" value="mailpasswordisgiven" />
  </appSettings>
Posted
Comments
Richard MacCutchan 20-Aug-15 8:03am    
You should capture the exception in order to find out what you did wrong.
Madhu Nair 20-Aug-15 8:05am    
if you can post the exception details then it will be helpful for others to find solution for your problem quickly.

1 solution

First of all, please do not write your configuration settings like that. Instead use the built-in settings placeholders for them.

XML
<system.net>
  <mailsettings>
    <smtp from="Sender's display name <sender_email@domain.com>">
      <network host="mailserver.yourdomain.com" port="25" username="smtp_server_username" password="secret" defaultcredentials="false" />
    </smtp>
  </mailsettings>
</system.net>

Credit to this SO thread[^].

Secondly the exception is incomplete, Failure sending mail. The actual exception comes later,

1. Remove name could not be resolved.
2. Unable to connect to remote server.

In real, they are all because of the only thing, connection was not established. Due to many factors. You should consider debugging the application, and see what is wrong. You must also consider giving a look at the exception message. It will let you know what you need to do to resolve the problem. Username/password problems comes later this stage, once the connection has been established. The exception in that case is "SMTP server requires a secure connection...". In your case, that is not yet the problem. :-)

You may be interested in reading this article of mine, Sending emails over .NET framework, and general problems – using C# code[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900