Click here to Skip to main content
15,891,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while sending it gives following error.

C#
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at 

----------

What I have tried:

---------------.aspx page-----------

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Gmail.aspx.cs" Inherits="FriendsEntreprises.Gmail" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

Sending Email


<asp:Label ID="lblStatus" runat="server" />


From:

<asp:TextBox ID="txtFrom" runat="server" Width="300">


Password:

<asp:TextBox ID="txtPassword" runat="server" Width="300" TextMode="Password">

To:

<asp:TextBox ID="txtTo" runat="server" Width="300">


Subject:

<asp:TextBox ID="txtSubject" runat="server" Width="300">


Message:

<asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Rows="5" Columns="100">



<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />

</form>
</body>
</html>
--------------------aspx.cs-------------------

using System;
using System.Net.Mail;

namespace FriendsEntreprises
{
public partial class Gmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtFrom.Focus();
}

protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress(txtFrom.Text.Trim());
objMailMessage.To.Add(new MailAddress(txtTo.Text.Trim()));
objMailMessage.Subject = txtSubject.Text.Trim();
objMailMessage.Body = txtMessage.Text.Trim();
objMailMessage.IsBodyHtml = false;
objMailMessage.Priority = MailPriority.High;

System.Net.NetworkCredential objNetworkCredential = new System.Net.NetworkCredential(txtFrom.Text.Trim(), txtPassword.Text.Trim());

SmtpClient objSmtpClient = new SmtpClient();
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

objSmtpClient.Host = "smtp.gmail.com";
objSmtpClient.Port = 587;

objSmtpClient.Credentials = objNetworkCredential;

objSmtpClient.Send(objMailMessage);

lblStatus.Text = "Email has been sent successfully!!!";
}
catch (SmtpException ex)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Error occured while sending your message." + ex.Message;
}
}
}
}
Posted
Updated 1-Sep-16 19:27pm
v3
Comments
F-ES Sitecore 2-Sep-16 4:29am    
This is one of the most commonly-asked questions. Google the error message and gamil and you'll get lots of ideas for a solution. You shouldn't be sending mail through gmail at all though, use the SMTP server provided by your webhost.

1 solution

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