i am trying to send email from my web app. i am using smtp gmail
for the same. but for some reasons i am not able to send a mail.. i have tried port numbers like 25,465,587 but none of these are working....
public static void SendMail(string host, string senderName, string frmAddress, string toAddress, int port, string subject, string messageText)
{
String smtpHost, port1;
smtpHost = ConfigurationManager.AppSettings["smtphost"].ToString();
port1 = ConfigurationManager.AppSettings["smtpport"].ToString();
SmtpClient mailClient = new SmtpClient(smtpHost, Convert.ToInt16(port1));
mailClient.EnableSsl = true;
NetworkCredential cred = new NetworkCredential();
cred.UserName = ConfigurationManager.AppSettings["username"].ToString();
cred.Password = ConfigurationManager.AppSettings["password"].ToString();
mailClient.Credentials = cred;
mailClient.UseDefaultCredentials = false;
//mailClient.Credentials = cred;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
try
{
MailAddress fromAddress = new MailAddress(frmAddress, senderName);
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = subject;
message.IsBodyHtml = false;
message.Body = messageText;
mailClient.Send(message);
//Response.Write("<script language='javascript' type='text/javascript'>window.alert('Email Successfully Sent.');</script>");
}
catch
{
//Response.Write("<script language='javascript' type='text/javascript'>window.alert('Send Email Failed '" + ex.Message + ");</script>");
}
}
calling this method:
protected void btnMail_Click1(object sender, EventArgs e)
{
string from = txtEmail.Text;
string to = "my gmail id";
string message = taMessage.InnerText;
Global.SendMail(ConfigurationManager.AppSettings["smtphost"].ToString(), txtName.Text, from, to, Convert.ToInt32(ConfigurationManager.AppSettings["smtpport"]), "Enquiry", taMessage.InnerText);
}
web.config settings :
<appSettings>
<add key="smtphost" value="smtp.gmail.com"/>
<!--<add key="smtphost" value="103.231.41.229"/>-->
<add key="smtpport" value="587"/>
<!--<add key="port" value="25"/>-->
<add key="username" value="my gmail id/>
<add key="password" value="my password"/>
<add key="From" value="my gmail id"/>
</appSettings>
Exception : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication