Click here to Skip to main content
15,888,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am try to send a mail and using this code but a error occurs again and again.it make me confuse please help me my code is


C#
protected void Button1_Click(object sender, EventArgs e)
{
    MailMessage mm = new MailMessage();
    mm.To.Add(txtto.Text);
    mm.From = new MailAddress(txtform.Text);
    mm.Subject = "My Simple mail";
    mm.Body = txtbody.Text;
    mm.IsBodyHtml = false;
    SmtpClient cmt = new SmtpClient();
    cmt.Host = ConfigurationManager.AppSettings["SMTP"];
    cmt.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
    cmt.DeliveryMethod = SmtpDeliveryMethod.Network;
    cmt.EnableSsl = true;
    cmt.Send(mm);
}



and Web config file::


XML
<appSettings>

    <add key="SMTP" value="smtp.gmail.com"/>

    <add key="FROMEMAIL" value="mail@gmail.com"/>

    <add key="FROMPWD" value="password"/>

  </appSettings>



and when i run the code so This error comes

"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"
Posted
Updated 31-Mar-12 9:19am
v2

The code below works for me... I have heard anecdotally that UseDefaulCredentials=false must precees the Credentials line

C#
var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout=10000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    try
    {
        smtp.Send(message);
     }
    catch (Exception e)
    {
        MessageBox.Show("Email request failed: " + e.Message);
    }
}
 
Share this answer
 
Please refer following url,

Send mail using Google Apps[^]

Hope this may help you.
 
Share this answer
 
try this

MailAddress mailfrom = new MailAddress ( "frommail@gmail.com" );
MailAddress mailto = new MailAddress ( "tomail@gmail.com" );
MailMessage newmsg = new MailMessage ( mailfrom, mailto );
newmsg.Subject = "Subject of Email";
newmsg.Body = "Body(message) of email";
////For File Attachment, more file can also be attached
Attachment att = new Attachment ( "G:\\code.txt" );
newmsg.Attachments.Add ( att );
SmtpClient smtps = new SmtpClient ( "smtp.gmail.com", 587 );
smtps.UseDefaultCredentials = false;
smtps.Credentials = new NetworkCredential ( "urmail@gmail.com", "urpwd" );
smtps.EnableSsl = true;
smtps.Send ( newmsg );
 
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