1
Reply

send mail to gmail acoount

kiran kumar

kiran kumar

May 21 2010 8:12 AM
3.8k
Hi this is kiran .. i try the code for send email  ..but i get error .please solve this code

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(Textfrom.Text, Textto.Text);
        mail.To.Add(Textto.Text);
        mail.From = new MailAddress(Textfrom.Text, "One Ghost", System.Text.Encoding.UTF8);
        mail.Subject = txtsubject.Text;
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = bodytxt.Text;
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;
        if (FileUpload1.HasFile)
        {
           // string PhysicalPath = "C:\\Documents and Settings\\Administrator\\Desktop\\";
            string fileName = FileUpload1.PostedFile.FileName;
            FileUpload1.PostedFile.SaveAs(fileName);

            Attachment data = new Attachment( fileName);
            mail.Attachments.Add(data);
        }
        SmtpClient client = new SmtpClient();
        //Add the Creddentials- use your own email id and password

        client.Credentials = new System.Net.NetworkCredential(Textfrom.Text, "9966794757");

        client.Port = 587; // Gmail works on this port
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true; //Gmail works on Server Secured Layers
        restxt.Text = "Send Mail";
        try
        {
            client.Send(mail);

        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            HttpContext.Current.Response.Write(errorMessage);
        }

But i got this error

Untitled PageSystem.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.gmail.com' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message

Answers (1)