1
Answer

send mail to gmail acoount

kiran kumar

kiran kumar

14y
3.8k
1
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)
0
Satyapriya Nayak
NA 53k 8m 12y
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Retrieving_excel_data_to_combobox
{
    public partial class Form1 : Form
    {
        public OleDbConnection con;
        public void pintu(string s)
        {
            con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 8.0;");

        }
        public OleDbCommand com;
        public DataSet ds;
        public OleDbDataAdapter oledbda;
        public DataTable dt;
        public string str;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnbrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog1 = new OpenFileDialog();
            openfiledialog1.ShowDialog();
            openfiledialog1.Filter = "allfiles|*.xls";
            TextBox1.Text = openfiledialog1.FileName;

        }

       

        private void btndisplay_Click(object sender, EventArgs e)
        {
            pintu(TextBox1.Text);
            try
            {
                con.Open();
                str = "select * from [sheet1$]";
                com = new OleDbCommand(str, con);
                ds = new DataSet();
                oledbda = new OleDbDataAdapter(com);
                oledbda.Fill(ds, "[sheet1$]");
                con.Close();
                DataGridView1.DataSource = ds;
                DataGridView1.DataMember = "[sheet1$]";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}

0
Tharake Gunatilake
NA 53 52.1k 12y
I got the answer guys..

 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofImport = new OpenFileDialog();
            ofImport.Title = "Select file";
            ofImport.InitialDirectory = @"c:\";
            ofImport.FileName = openFileDialog1.FileName;
            ofImport.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
            ofImport.FilterIndex = 1;
            ofImport.RestoreDirectory = true;


            if (ofImport.ShowDialog() == DialogResult.OK)
            {

                string path = System.IO.Path.GetFullPath(ofImport.FileName);

                string query = "SELECT * FROM [Sheet1$]";

                OleDbConnection conn = new OleDbConnection();

                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ofImport.FileName + ";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";

                OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);

                DataSet dataSet = new DataSet();

                adapter.Fill(dataSet);

                dataGridView1.DataSource = dataSet.Tables[0];


            }
            else
            {
                ofImport.Dispose();
            }   
        }