6
Reply

Problem while sending mail

Ankit Kumar

Ankit Kumar

Oct 6 2016 3:32 AM
246
Hello Guys,
 
i am having problem while writing codes to implement mail messeging in asp .net with c#
 
i am using following code 
 
try{
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
}
catch(Exception ex)
{
Response.Write("<script>alert("+ex.Message+")</script>");
}
 
 
 
The error i am facing is
 
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. ra13sm19230672pac.29 - gsmtp 
 
 

Answers (6)