datagrid将数据作为邮件附件查看 [英] datagrid view data as mail attachment
本文介绍了datagrid将数据作为邮件附件查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
}
无法附加消息get 对象引用未设置为对象实例
not able to attach message get object reference not set to an instance of object
推荐答案
如果您使用.NET Framework 2.0版,或者更新:MailAttachment已过时.您应该使用附件对象.
请参阅 http://msdn.microsoft.com/en-us/library/6sdktyws(v = VS.80).aspx [
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[^]
您需要初始化myMessage.Attachments
.
在将对象添加到引用之前,将其设置为引用.
You need to initializemyMessage.Attachments
.
Set this to a reference before adding an object to it.
这篇关于datagrid将数据作为邮件附件查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文