I want to send email from smtp gmail server.iam using http location.
I found this error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. os1sm57959508pac.20 - gsmtp"
How to solve this..??
My page and code is as follows
code behind (aspx.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
public partial class smtpcredinwebconfig : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage ObjMailMessage = new MailMessage();
ObjMailMessage.From = new MailAddress(ConfigurationManager.AppSettings["FromMail"].ToString(), "ASP.NET Mail");
ObjMailMessage.To.Add(new MailAddress(txtTo.Text.Trim()));
ObjMailMessage.Subject = txtSubject.Text.Trim();
ObjMailMessage.Body = txtMsg.Text.Trim();
if (fUAttachment.HasFile)
{
ObjMailMessage.Attachments.Add(new Attachment(fUAttachment.PostedFile.InputStream, fUAttachment.PostedFile.FileName));
}
ObjMailMessage.IsBodyHtml = false;
ObjMailMessage.Priority = MailPriority.High;
SmtpClient ObjSmtpClient = new SmtpClient();
ObjSmtpClient.Host = ConfigurationManager.AppSettings["Host"].ToString();
ObjSmtpClient.Port = int.Parse(ConfigurationManager.AppSettings["Port"].ToString());
ObjSmtpClient.Send(ObjMailMessage);
lblStatus.Text = "Mail Sent Successfully";
lblStatus.ForeColor = System.Drawing.Color.Green;
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
lblStatus.ForeColor = System.Drawing.Color.Red;
}
}
}
Source View(aspx):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="smtpcredinwebconfig.aspx.cs" Inherits="smtpcredinwebconfig" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td colspan="2"><asp:Label ID="lblStatus" runat="server"></asp:Label></td></tr>
<tr>
<td>To</td>
<td><asp:Textbox ID="txtTo" runat="server"></asp:Textbox></td>
</tr>
<tr>
<td>Subject</td>
<td><asp:Textbox ID="txtSubject" runat="server"></asp:Textbox></td>
</tr>
<tr>
<td>Message</td>
<td><asp:Textbox ID="txtMsg" runat="server" TextMode="MultiLine"></asp:Textbox></td>
</tr>
<tr>
<td>Attach Files</td>
<td><asp:FileUpload ID="fUAttachment" runat="server" /></td>
</tr>
<tr><td></td><td><asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click"></asp:Button></td></tr>
</table>
</div>
</form>
</body>
</html>
Web.config:
="1.0"="utf-8"
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<identity impersonate="false" />
</system.web>
<appSettings>
<add key="FromMail" value="vinod475.2009@gmail.com" />
<add key="Host" value="smtp.gmail.com" />
<add key="Port" value="587" />
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network defaultCredentials="false" host="smtp.gmail.com" password="& port="587" userName="vinod475.2009@gmail.com" />
</smtp>
</mailSettings>
</system.net>
</configuration>