Sending email error using asp.net c# web service?

-1

I have created a web service in asp.net c#. I am trying to send email but email not send successfully.I am using gmail account for sending email.But its throws error The transport failed to connect to the server . I don't know where is the problem,everything looking correct.

     [WebMethod]
       public string sendMail(string sendMailAddress, string eventID, 
       ArrayList cart)
        {
              string msg = "";
             string sourcePath;
            try
            {
            System.Web.Mail.MailMessage mail = new 
            System.Web.Mail.MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = "test@gmail.com";
            mail.To = sendMailAddress;
            mail.Subject = "Test Mail";
            mail.Body = "Your event id is :" + eventID;
            ArrayList myList = new ArrayList();
            foreach (var item in cart)
            {
                myList.Add(item);
            }

            string[] filePaths = Directory.GetFiles(@"E:\images");
            IEnumerable<string> filess = Enumerable.Empty<string>();
            // Get all the files in the sourceDir
            foreach (string name in myList)
            {
                string fname = name;
                sourcePath = "E:\\images\\" + fname;
                if (System.IO.File.Exists(sourcePath))
                {
                    var attachment = new Attachment(sourcePath);
                    mail.Attachments.Add(attachment);
                }

            }
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password");
            SmtpServer.EnableSsl = true;
            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(mail);
            msg = "Success";


        }
        catch (Exception ex)
        {
            msg = ex.InnerException.ToString();
        }
        return msg;

    }

It throwing follow error

{System.Web.HttpException (0x80004005): The transport failed to connect to the server. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: The transport failed to connect to the server.

c#
asp.net
sendmail
asked on Stack Overflow Apr 9, 2019 by icon

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0