2013-07-19 110 views
0

我使用下面的代碼來生成PDF並將其保存到某個位置。將附帶生成的PDF作爲電子郵件發送出去是否可能?我假設電子郵件編碼需要在HTML中完成?因爲它會在網絡服務器上。這可能嗎?發送電子郵件與生成PDF作爲附件?

Dim Doc1 As New Document 
    Dim path As String = "\\Server\Folder" + Session("Username") + "\" 
    If (Not System.IO.Directory.Exists(path)) Then 

     System.IO.Directory.CreateDirectory(path) 
    End If 
    Dim myUniqueFileName = String.Format("{0}.pdf", random) 
    Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create)) 
    ' Dim ev As New itsEvents 
    ' pdfWrite.PageEvent = ev 

    Doc1.Open() 
    Dim test As String 
    test = Session("PDF") 
    Dim imagepath As String = Server.MapPath(".") & "/images/Header.png" 
    Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath) 
    image.ScalePercent(70.0F) 
    ' image.SetAbsolutePosition(36.0F, 36.0F) 
    Doc1.Add(image) 
    Doc1.Add(New Paragraph(test)) 

    Doc1.Close() 
+1

你進去看了'System.Net.Mail'命名空間? http://msdn.microsoft.com/en-us/library/System.Net.Mail.aspx –

+0

是的,但我不知道我將如何抓住附件? – user1342164

+0

http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx有構造函數需要一個字符串,有些需要流。你應該能找到一個適合你的人。 –

回答

0

試試這個:

' Create the mail message 
Dim mail As New MailMessage() 

' Set the addresses 
mail.From = New MailAddress("[email protected]") 
mail.To.Add("[email protected]") 

' Set the content 
mail.Subject = "This is an email" 
mail.Body = "this content is in the body" 

' Get some binary data 
Dim data As Byte() = GetData() 

' Save the data to a memory stream 
Dim ms As New MemoryStream(data) 

' Create the attachment from a stream. Be sure to name the data with a file and 
' media type that is respective of the data 
mail.Attachments.Add(New Attachment(ms, "example.txt", "text/plain")) 

' Send the message 
Dim smtp As New SmtpClient("127.0.0.1") 
smtp.Send(mail) 

Function GetData() As Byte() 
    ' This is where you will load your data from disk or database, etc. 
    Dim s As String = "this is some text" 
    Dim data As Byte() = Encoding.ASCII.GetBytes(s) 
    Return data 
End Function 'GetData 
+0

在下面的代碼行中,我將如何指向附件?說出它是否在\\ Server1 \ Attachments \上? mail.Attachments.Add(新附件(ms,「example.txt」,「text/plain」)) – user1342164

相關問題