本文介绍了datagrid将数据作为邮件附件查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void button2_Click(object sender, EventArgs e)
{
MailMessage myMessage = new MailMessage();
try
{
myMessage.From = "[email protected]";//place here from address
myMessage.To = "[email protected]";//place here to address
myMessage.Cc = "[email protected]";//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);
}
无法附加消息get 对象引用未设置为对象实例
not able to attach message get object reference not set to an instance of object
推荐答案
这篇关于datagrid将数据作为邮件附件查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!