private void button2_Click(object sender, EventArgs e) { MailMessage myMessage = new MailMessage(); try { myMessage.From = "from@yourDomain.com";//place here from address myMessage.To = "to@yourDomain.com";//place here to address myMessage.Cc = "cc@someDomain.com";//place here copy address myMessage.BodyEncoding = Encoding.UTF8; myMessage.BodyFormat = MailFormat.Html; //call method, creating HTML from datagridview myMessage.Body ="test"; //place here your subject myMessage.Subject = "message subject"; StreamWriter sw = new StreamWriter("file.html", true, Encoding.UTF8);//creating html file sw.Write(htmlMessageBody(dataGridView1).ToString());//write datagridview contents to HTML file sw.Close(); MailAttachment myAttach = new MailAttachment("file.html");//create attachment myMessage.Attachments.Add(myAttach);//add attachment to our message //if it is needed set up credentials myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//Basic Authentication myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2"); myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "user name");//user name myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");//password //place here SMTP server myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "port"); SmtpMail.SmtpServer = "your SMTP server"; myMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true); SmtpMail.Send(myMessage); MessageBox.Show("Message sent!"); } catch (Exception ex) { MessageBox.Show("Error! " + ex.Message); }
myMessage.Attachments
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)