Error when sending email system.web.httpexceptions: the message could not be sent to the smtp server

2

I have a function in my program that sends an email from a google account.

I wrote this code a while back and it used to work fine, it sends email just as it is supposed to. Now, however, I could not get it to work. (I am now working on Windows 7 64-bit, if that would make a difference).

The error I get (which is the first error message from the code) is:

system.web.httpexceptions: the message could not be sent to the smtp server. the transport error code was 0x80040217. the server response was not available --> system.reflection.targetinvocationexception: exception has been thrown by the target of an invocation. --> system.runtime.interopservice.comexception (0x80040211): the message could not be sent to the smtp server. the transport error code was 0x80040217. the server response was not available.

This is the code:

void sendEmail(string [] emailList, int emailLength, string fileName)
{
    int i = 0;      //variable to act as temporary index into emailList array
    try
    {
        try
        {
            MailMessage message = new MailMessage();
        //Because using google server, requires SSL
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
            message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
            message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","theusername@gmail.com" );
            message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","thepassword" );

            message.From = "theusername@gmail.com";
            message.To = emailList[i];

            //add Bcc
            while (++i < emailLength)
            {
                message.Bcc = emailList[i];
            }//end while

            message.Subject = "Test Subject";
            message.Body = "Test Body";

            MailAttachment fileAttach = new MailAttachment(fileName);
            message.Attachments.Add(fileAttach);

            try
            {       
                SmtpMail.SmtpServer = "smtp.gmail.com";

Thank you in advance!

c#
email
smtp
asked on Stack Overflow Apr 6, 2012 by Favn Hghksd • edited Apr 6, 2012 by James Wiseman

2 Answers

1

I had the same problem, but solved it when I used a Gmail address where I did not use the 2-step verification of my Gmail login. If you only use a password without the extra verification (in my case via a mobile phone code), it works

answered on Stack Overflow Sep 16, 2013 by Hanlie Croeser
1

maybe you have been blocked by google because you are sending from a program in a remote computer ( that is not the one you are usually work with ) try open gmail in the same computer that runs the program., see if you have security question.

on the gmail account go to https://www.google.com/settings/account and there open the security and see i gmail has blocked the access to your account

answered on Stack Overflow Nov 12, 2013 by nalizo

User contributions licensed under CC BY-SA 3.0