Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将我的pdf放入Mail atachment_C# - Fatal编程技术网

C# 如何将我的pdf放入Mail atachment

C# 如何将我的pdf放入Mail atachment,c#,C#,我需要一个代码帮助,使我的.pdf文件的atachment电子邮件。 我试图找到一个解决办法,但找不到。 对不起,我英语不好 这是我的pdf创建代码 SaveFileDialog dialog1 = new SaveFileDialog(); dialog1.Title = "Saving pdf "; dialog1.Filter = "PDF Files (*.pdf)|*.pdf|All files (*.*)|*.*";

我需要一个代码帮助,使我的.pdf文件的atachment电子邮件。 我试图找到一个解决办法,但找不到。 对不起,我英语不好

这是我的pdf创建代码

SaveFileDialog dialog1 = new SaveFileDialog();
            dialog1.Title = "Saving pdf ";
            dialog1.Filter = "PDF Files (*.pdf)|*.pdf|All files (*.*)|*.*";
            dialog1.RestoreDirectory = true;
            if (dialog1.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(dialog1.FileName);
            }
           /*  DialogResult result = folderBrowserDialog1.ShowDialog();
             if (result == DialogResult.OK)
             {
                 string caminho = folderBrowserDialog1.SelectedPath;
                 var pasta2 = caminho.Replace(@"\", @"\\");*/
                 Document doc = new Document(PageSize.A4.Rotate(), 10, 10, 42, 35);
                 PdfWriter writertest = PdfWriter.GetInstance(doc, new FileStream(dialog1.FileName, FileMode.Create));
                 doc.Open();
                 PdfPTable table = new PdfPTable(itemDataGridView.Columns.Count);
                 for (int j = 0; j < itemDataGridView.Columns.Count; j++)
                 {
                     table.AddCell(new Phrase(itemDataGridView.Columns[j].HeaderText));
                 }
                 table.HeaderRows = 1;
                 for (int i = 0; i < itemDataGridView.Rows.Count; i++)
                 {
                     for (int k = 0; k < itemDataGridView.Columns.Count; k++)
                     {
                         if (itemDataGridView[k, i].Value != null)
                         {
                             table.AddCell(new Phrase(itemDataGridView[k, i].Value.ToString()));
                         }
                     }
                 }
                 doc.Add(table);
                 doc.Close();
SaveFileDialog dialog1=新建SaveFileDialog();
对话框1.Title=“保存pdf”;
dialog1.Filter=“PDF文件(*.PDF)|*.PDF |所有文件(*.*)|*.”;
dialog1.RestoreDirectory=true;
如果(dialog1.ShowDialog()==DialogResult.OK)
{
MessageBox.Show(dialog1.FileName);
}
/*DialogResult=folderBrowserDialog1.ShowDialog();
if(result==DialogResult.OK)
{
字符串caminho=folderBrowserDialog1.SelectedPath;
var pasta2=caminho.Replace(@“\”,@“\”)*/
Document doc=新文档(PageSize.A4.Rotate(),10,10,42,35);
PdfWriter writertest=PdfWriter.GetInstance(doc,newfilestream(dialog1.FileName,FileMode.Create));
doc.Open();
PdfPTable table=新的PdfPTable(itemDataGridView.Columns.Count);
对于(int j=0;j
这是我的电子邮件发送代码

 Pesquisar_Items pesquisar = new Pesquisar_Items();
            var client = new SmtpClient("smtp.live.com", 25);
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential("josepedrobrito@hotmail.com", "*******");
            var mail = new MailMessage();
            mail.From = new MailAddress("josepedrobrito@hotmail.com");
            mail.To.Add(textBox1.Text);
            mail.IsBodyHtml = true;
            mail.Subject = textBox2.Text;
            string mailBody = "<table width='100%' style='border:Solid 1px Black;'>"; ;
            foreach (DataGridViewRow row in itemDataGridView.Rows)
            {
                mailBody += "<tr>";
                foreach (DataGridViewCell cell in row.Cells)
                {
                    mailBody += "<td>" + cell.Value + "</td>";
                }
                mailBody += "</tr>";
            }
            mailBody += "</table>";
            client.Send(mail);
            MessageBox.Show("O email send ");
            this.Close();
Pesquisar_项目Pesquisar=新的Pesquisar_项目();
var client=新的SmtpClient(“smtp.live.com”,25);
client.enablesl=true;
client.Credentials=新的网络凭据(“josepedrobrito@hotmail.com", "*******");
var mail=new MailMessage();
mail.From=新邮件地址(“josepedrobrito@hotmail.com");
mail.To.Add(textBox1.Text);
mail.IsBodyHtml=true;
mail.Subject=textBox2.Text;
字符串mailBody=“”;
foreach(itemDataGridView.Rows中的DataGridViewRow行)
{
邮件正文+=“”;
foreach(row.Cells中的DataGridViewCell单元格)
{
邮件正文+=“”+单元格.值+“”;
}
邮件正文+=“”;
}
邮件正文+=“”;
客户端。发送(邮件);
MessageBox.Show(“O电子邮件发送”);
这个。关闭();
您创建一个,然后将其添加到
附件
集合:

// Create the attachment.
Attachment data = new Attachment(file, MediaTypeNames.Application.Pdf);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
file
是从
FileSaveDialog
返回的要附加到电子邮件的文件的路径名

你可能还想做更多的事情,比如添加时间戳信息等,你需要打电话

data.Dispose();
在你发送信息之后

当您使用代码创建文件时,您可以将其保存到临时目录,然后在发送电子邮件后将其从磁盘中删除,而无需用户查看对话框或输入文件名

只需附加内存流,即可直接从内存创建附件:

using (MemoryStream memoryStream = new MemoryStream())
{
    PdfWriter writertest = PdfWriter.GetInstance(doc, memoryStream);
    // Write contents of Pdf here

    // Set the position to the beginning of the stream.
    memoryStream.Seek(0, SeekOrigin.Begin);

    // Create attachment
    ContentType contentType = new ContentType();
    contentType.MediaType = MediaTypeNames.Application.Pdf;
    contentType.Name = fileNameTextBox.Text;
    Attachment attachment = new Attachment(memoryStream, contentType);

    // Add the attachment
    message.Attachments.Add(attachment);

    // Send Mail via SmtpClient
    smtpClient.Send(message);
}

您创建一个,然后将其添加到
附件
集合:

// Create the attachment.
Attachment data = new Attachment(file, MediaTypeNames.Application.Pdf);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
file
是从
FileSaveDialog
返回的要附加到电子邮件的文件的路径名

你可能还想做更多的事情,比如添加时间戳信息等,你需要打电话

data.Dispose();
在你发送信息之后

当您使用代码创建文件时,您可以将其保存到临时目录,然后在发送电子邮件后将其从磁盘中删除,而无需用户查看对话框或输入文件名

只需附加内存流,即可直接从内存创建附件:

using (MemoryStream memoryStream = new MemoryStream())
{
    PdfWriter writertest = PdfWriter.GetInstance(doc, memoryStream);
    // Write contents of Pdf here

    // Set the position to the beginning of the stream.
    memoryStream.Seek(0, SeekOrigin.Begin);

    // Create attachment
    ContentType contentType = new ContentType();
    contentType.MediaType = MediaTypeNames.Application.Pdf;
    contentType.Name = fileNameTextBox.Text;
    Attachment attachment = new Attachment(memoryStream, contentType);

    // Add the attachment
    message.Attachments.Add(attachment);

    // Send Mail via SmtpClient
    smtpClient.Send(message);
}

我知道,我已经看到了这一点,但我无法在我的代码中实现……在发送邮件之前,你并没有将
mailBody
添加到
mail
对象中。这只是问题中的一个剪切粘贴错误吗?@JoséPedroBrito:为什么不?你尝试了什么?你有什么问题吗?我知道,我已经看到了,但我无法即时通信在我的代码中添加一个字符…在发送之前,你没有将
邮件体
添加到
邮件
对象中。这只是问题中的一个剪切粘贴错误吗?@JoséPedroBrito:为什么不?你尝试了什么?你有什么问题吗?我能把我的pdf放进去吗?@JoséPedroBrito:看看
的构造函数和静态方法ttachment
class.@Chris:文件不需要存在于磁盘上。@SLaks-好的-糟糕的措辞-我会删除它。我能把我的pdf放在里面吗?@JoséPedroBrito:看看
附件
类的构造函数和静态方法。@Chris:文件不需要存在于磁盘上。@SLaks-好的-糟糕的措辞-我会删除它。