我已经从gridview生成了一个PDF文件。我的任务是将生成的PDF文件发送到特定的电子邮件,而无需用户使用上传控件并选择我生成的PDF文件。我能够通过用户手动浏览并选择PDF作为附件来执行普通电子邮件,有没有办法实现PDF文件到电子邮件的自动附件?
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT Id, msgContent, msgDate from Message where msgDate between '" + dateFrom + "' and '" + dateTo + "'";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
这是我绑定到gridview的数据。
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlworker = new HTMLWorker(pdfDoc);
using (MemoryStream ObjememoryStream = new MemoryStream())
{
PdfWriter.GetInstance(pdfDoc, ObjememoryStream);
pdfDoc.Open();
htmlworker.Parse(sr);
pdfDoc.Close();
byte[] Filebytes = ObjememoryStream.ToArray();
ObjememoryStream.Close();
using (MemoryStream inputData = new MemoryStream(Filebytes))
{
using (MemoryStream outputData = new MemoryStream())
{
string PDFFileword = txtPassword.Text;//you can also generate Dynamic word
PdfReader reader = new PdfReader(inputData);
PdfEncryptor.Encrypt(reader, outputData, true, PDFFileword, PDFFileword, PdfWriter.ALLOW_SCREENREADERS);
Filebytes = outputData.ToArray();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(Filebytes);
Response.End();
GridView1.AllowPaging = true;
GridView1.DataBind();
}
}
}
这是我生成PDF文件的方式。
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
这是我通过上传控制附件文件发送电子邮件的方式。 " fuAttachment"是我上传控件的ID,有没有办法让附件自动生成?任何帮助表示赞赏,谢谢先进的人。
答案 0 :(得分:1)
您只需要创建新的Attachment
实例,并将其添加到mailmessage实例中的attatchments集合中。
如果您有文件存储在硬盘驱动器上并且您知道路径:
Attachment attachment = new Attachment("FILE_PATH",
MediaTypeNames.Application.Octet);
mm.Attachments.Add(attachment);
如果你在内存中有字节数组,那么:
Stream stream = new MemoryStream(byteArray);
Attachment attachment = new Attachment(stream, MediaTypeNames.Application.Octet);
mm.Attachments.Add(attachment);
如果你有流:
Attachment attachment = new Attachment(fileStream, MediaTypeNames.Application.Octet);
mm.Attachments.Add(attachment);