简体   繁体   中英

Sending email using gmail smtp server

I have been trying for a long time to send a mail from a Gmail account to a gmail account using the below code.

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);
}

After the execution reaches "smtp.Send(mm)" the browser says waiting and after 2 minutes I get the exception saying "Failure Sending Email"

And the following Error message

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond . . . :587" (some IP)

I have searched a lot for this but haven't found a solution. Please help me solve this issue.

Thank you.

First of all, I think you should use

UseDefaultCredentials = false;

And

smtp.DeliveryMethod = SmtpDeliveryMethod.Network

You also need to allow less secure apps to access your account

Try This One.

public static string SendMail(string stHtmlBody, string stSubject, string stToEmailAddresses)
{
    string stReturnText = string.Empty;
    try
    {
        if (!string.IsNullOrEmpty(stToEmailAddresses))
        {
            //Set SmtpClient to send Email
            string stFromUserName = "fromusername";
            string stFromPassword ="frompassword";
            int inPort = Convert.ToInt32(587);
            string stHost = "smtp.gmail.com";
            bool btIsSSL =true;



            MailAddress to = new MailAddress(stToEmailAddresses);
            MailAddress from = new MailAddress("\"" + "Title" + "\" " + stFromUserName);

            MailMessage objEmail = new MailMessage(from, to);
            objEmail.Subject = stSubject;
            objEmail.Body = stHtmlBody.ToString();
            objEmail.IsBodyHtml = true;
            objEmail.Priority = MailPriority.High;


            SmtpClient client = new SmtpClient();
            System.Net.NetworkCredential auth = new System.Net.NetworkCredential(stFromUserName, stFromPassword);
            client.Host = stHost;
            client.Port = inPort;
            client.UseDefaultCredentials = false;
            client.Credentials = auth;
            client.EnableSsl = btIsSSL;
            client.Send(objEmail);

            return stReturnText;
        }
    }
    catch (Exception ex)
    {

    }

    return stReturnText;
}

I had a similar attempt but using Java. I was not successful doing that after a lot of search. I then used the Yahoo! SMTP, which worked very easily. Maybe you can try that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM