Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
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);
            }


not able to attach message get object reference not set to an instance of object
Posted
Updated 11-Aug-11 22:47pm
v6
Comments
Herman<T>.Instance 12-Aug-11 3:41am    
try this?

If you use version 2.0 of the .NET framework or newer: The MailAttachment is obsolete. you should use Attachment object.

see http://msdn.microsoft.com/en-us/library/6sdktyws(v=VS.80).aspx[^]
 
Share this answer
 
You need to initialize myMessage.Attachments.
Set this to a reference before adding an object to it.
 
Share this answer
 
Comments
Member 7925220 12-Aug-11 3:49am    
MailAttachment myAttach = new MailAttachment("file.html");//create attachment
myMessage.Attachments.Add(myAttach);//add attachment to our message

allready do that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900