Click here to Skip to main content
14,733,013 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//My mail function is always showing the exception message of: sending mail failure......//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Net;
namespace housing1
public partial class Ucomplain : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("Select Name from Standard_Complain", conn);
DataTable dt = new DataTable();
DropDownListTOC.DataSource = dt;
DropDownListTOC.DataTextField = "Name";

protected void Button1_Click(object sender, EventArgs e)

MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(TextBoxPME.Text);
Msg.Body = TextBoxD.Text;
Msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("", 587);
client.Credentials = new NetworkCredential(TextBoxPME.Text , "nid100");

client.EnableSsl = true;

Response.Write("mail send");

catch(Exception ex)


protected void Button2_Click(object sender, EventArgs e)
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);

String str = "select Email from Primary_Member where Flat='" + TextBoxF.Text.Trim() + "'";
SqlCommand comm = new SqlCommand(str, conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
TextBoxPME.Text = reader["Email"].ToString();

Jochen Arndt 19-Oct-15 3:14am
Does the error message contain more information?
You should show the complete message.
sreeyush sudhakaran 19-Oct-15 3:19am
1) If you are trying to Send Mail through gmail , you need to add your application to google trusted list or turnoff the blocking of sending emails from untrusted Apps from your gmail account.

Please see here how to Turnoff it :

Have Look at SmtpClient.Send Method in MSDN for more or try SendAsync Method
Richard Deeming 20-Oct-15 11:13am
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

//Read fromEmail from registery
string fromEmail = Registery.fromAddress;

//Read fromPassword from registery
string fromPassword = Registery.password;

//Read toAddress from registery
string toAddress = Registery.ToAddress;

//Read outgoing server name from registery
string smtpServer = Registery.smtpServer;

//Read port of outgoing server from registery
int port = Convert.ToInt32(Registery.port);

//Read whether enableSSL is true or false from registery
bool enableSSL = Convert.ToBoolean(Registery.enableSSL);

message.From = new MailAddress(fromEmail.ToString());
message.Subject = ServiceName + " " + status.ToString();
message.Body = "Service Name :" + ServiceName + Environment.NewLine + "Status :" + status.ToString();
message.IsBodyHtml = true;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

///smtp server and port configured at registry
SmtpClient smtpClient = new SmtpClient(smtpServer, port);

///enable ssl is required for secure connection.It is must be true for gmail server and false for other servers.
smtpClient.EnableSsl = enableSSL;

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail,fromPassword);

catch (Exception ex)

Logger.Log("Error In Sending Mail. " + ex.Message + " , " + ex.InnerException, 3);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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