0

I cannot send email with this code, my label showed 'failure sending mail'. Pls help me. yemail = sender's email. Email = recepient's email. emailp = Sender's Password

    using System.Net;
using System.Net.Mail;

protected void end_Click(object sender, EventArgs e)
{
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress(yemail.Text, "Sender's Name");
    msg.To.Add(new MailAddress(Email.Text));
    msg.Subject = Subject.Text;
    msg.Body = Message.Text;
    msg.IsBodyHtml = true;

    if(FileUpload1.HasFile)
    {
        msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));

    }

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Credentials = new NetworkCredential(yemail.Text, emailp.Text);
    Label1.Visible = true;
    try
    {
        smtp.Send(msg);
        Label1.Text = "Email Send successfully";

    }
    catch (Exception ex)
    {
        Label1.Text = ex.Message;
    }

2 Answers2

0

Be sure your gmail configuration is ok. Click here and chose "Open".

And add port to your codes as this;

    smp.Port = 587;
    smp.EnableSsl = true;
0

Browse to the account settings, locate the SMTP request in its Recent activity, and select "This was me":

enter image description here