Click here to Skip to main content
16,016,643 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();
da.Fill(dt);
DropDownListTOC.DataSource = dt;
DropDownListTOC.DataTextField = "Name";
DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
try
{

MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(TextBoxPME.Text);
Msg.To.Add("sunita@indweb.com");
Msg.Body = TextBoxD.Text;
Msg.IsBodyHtml = true;

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

client.EnableSsl = true;

client.Send(Msg);
Response.Write("mail send");
}

catch(Exception ex)
{
Response.Write(ex.Message);
}


}

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

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();
reader.Close();
}
}



}
}
Posted
Comments
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 : http://how2doinvbdotnet.blogspot.ae/2015/06/send-email-from-gmail-with-smtp.html

http://how2doinvbdotnet.blogspot.ae/2015/10/asynchronous-parallel-mail-sending.html

Have Look at SmtpClient.Send Method in MSDN https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx 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.To.Add(toAddress.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);
smtpClient.Send(message);
}

catch (Exception ex)
{

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

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