Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,

Here is my code. While iam executing this got the below error. Please anyone help on this issue.

Error"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated "


Code:

C#
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
               SmtpClient smtpClient = new SmtpClient();
               MailAddress fromAddress = new MailAddress(From, displayName);
                          message.From = fromAddress;
               message.Subject = "Test Mail in SMTP";
               message.To.Add(To);
               smtpClient.Host = "smtp";
               smtpClient.Port = 25;
               smtpClient.UseDefaultCredentials = false;
               smtpClient.Credentials = new System.Net.NetworkCredential("Mail", "pwd");
               //smtpClient.EnableSsl = true;
               System.Net.NetworkCredential auth = new System.Net.NetworkCredential("Mail", "pwd");
               smtpClient.Credentials = auth;
               smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
               smtpClient.Send(message);
Posted
Updated 18-Feb-15 21:24pm
v2

C#
using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
 
Share this answer
 
v2
The error says either your username or passwoord is incorrect,or enable the ssl
 
Share this answer
 
Use this code...


C#
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Globalization;

string to = "rajeeshmenoth@wordpress.com";//To address
string from = "rajeeshmenoth@wordpress.com";//From address
MailMessage message = new MailMessage(from, to);

if (fuAttachment.HasFile)//Attaching document
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            message .Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
message.Subject = txt_subject.text;
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("yourmail id", "Password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
throw ex;
}


or
HTML
smtpClient.EnableSsl = true;//enable the ssl
 
Share this answer
 
v2

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