Problems sending e-mail in web app

久未见 提交于 2019-12-12 02:57:07

问题


I have been trying for 2 days to get my ASP.NET webforms application to send an e-mail.

I have tried this using both outlook and gmail. I got the smtp information for both from this tutorial:

When I try using port 587 in the example below I get an error saying: An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

When I try using port 465 in the example below: My application just hangs forever and my page never gets back my email function which is in the PageLoad.

A couple of things to note just incase one of these is messing me up:

  1. I am using a standard VS2013 dev environment (running my web app in debug mode using F5)
  2. I am using the same e-mail address for from, to, and gmail login credentials)
  3. My ISP does not block these ports. (comcast cable)
  4. I even went to the google DisplayUnlockCaptcha page to Allow access to my account

    protected void SendEmail()
    {
        string EmailAddress = "myemail@gmail.com";
        using (MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress))
        {
            mailMessage.Subject = "This is a test email";
            mailMessage.Body = "This is a test email. Please reply if you receive it.";
    
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = true;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
    
            smtpClient.Credentials = new System.Net.NetworkCredential()
            {
                UserName = EmailAddress,
                Password = "password"
            };
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Send(mailMessage);
            }
    }
    

回答1:


This code should work fine for you

protected void SendEmail()
{
    string EmailAddress = "myemail@gmail.com";
    MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
    mailMessage.Subject = "This is a test email";
    mailMessage.Body = "This is a test email. Please reply if you receive it.";

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.EnableSsl = true;
    smtpClient.Host = "smtp.gmail.com";
    smtpClient.Port = 587;

    smtpClient.Credentials = new System.Net.NetworkCredential()
    {
        UserName = EmailAddress,
        Password = "password"
    };
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Send(mailMessage);
}

You'll need to set the delivery mode otherwise gmail will return an error

EDIT:

Throwing an 'using' around 'MailMessage' might also be a smart thing to do




回答2:


It turns out that it is because of a GMail security setting. https://www.google.com/settings/security/lesssecureapps

You have to enable access for less secure apps.




回答3:


public  void sendEmail(string body)
    {
        if (String.IsNullOrEmpty(email))
            return;
        try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(email);
            mail.To.Add("xxx@gmail.com");
            mail.From = new MailAddress("yyy@gmail.com");
            mail.Subject = "sub";

            mail.Body = body;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential
                 ("yyy@gmail.com", "pw"); // ***use valid credentials***
            smtp.Port = 587;

            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            print("Exception in sendEmail:" + ex.Message);
        }
    }``

http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/



来源:https://stackoverflow.com/questions/26113404/problems-sending-e-mail-in-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!