简体   繁体   中英

Error while sending an email with System.Net.Mail. What to look for when debugging?

Here's the code I'm running:

try
{
    var fromAddress = new MailAddress("foo@ree.com", "foo");
    var toAddress = new MailAddress("test@gmail.com", String.Empty);
    const string fromPassword = "password";
    const string subject = "Welcome!";
    const string body = "This is a test message!";

    var smtp = new SmtpClient
    {
        Host = "mail.foo.com",
        Port = 143,
        EnableSsl = false,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
    };
    using (var message = new MailMessage(fromAddress, toAddress)
                                        {
                                            Subject = subject,
                                            Body = body
                                        })
    {
        smtp.Send(message);
    }
}
catch (Exception e)
{
    Debug.WriteLine(e.InnerException.ToString());
    //Add logging here.
}

When I try to run smtp.Send(message) the following error is fired:

A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
    System.FormatException: Smtp server returned an invalid response.
    at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read,  Boolean readLine)
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
    at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
    at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)

What are the probably causes for this error? What should I be looking for?

Are you sure your SMTP port is 143? Default is 25. It looks like it can't talk with the server correctly first (since the error is in GetConnection).

If there is no problem with your SMTP script, but you still got the message message mentioned above, it should because Gmail blocked the authentication from our server as it detected that it is the first time you login to your Gmail account from another Country or Location. You will need to Login to gmail security center to approve the authntication. Once you approved it ,

please wait a few minutes then sending email from script again. Here are the steps to approve the "Unusual activity alerts" from gmail security center.

a) go to gmail security center via this link blow or Google search for "gmail security" and login with your gmail account https://accounts.google.com/ServiceLogin?elo=1

b) next to "security" / "Recent activity" , click to "view all events"

c) You will able to see "Unusual Activity" , it will show all unusual activity events, select related event and approval it via click " Yes, That was me!"

try
{
    MailMessage mail = new MailMessage();
    mail.To.Add("yourid@gmail.com");
    mail.CC.Add("ccid@hotmail.com");
    mail.From = new MailAddress("from@gmail.com");
    mail.Subject = "Feedback for Website";
    string Body = "Name:"+TextBoxName.Text+" Phone Number: "+TextPhone.Text;
    mail.Body = Body;
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    smtp.Credentials = new System.Net.NetworkCredential("your@gmail.com", "pass");
    smtp.Send(mail);
    lblStatus.Text = "Message send successfully.";
    txtMessage.Text = "";
}
catch (Exception ex)
{
    lblStatus.Text = ex.ToString();
}

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