Adds the attachment from a stream.
true if the attachment was successfully added to the collection; otherwise, false.
The targetFilename parameter allows the developer to override the filename under which the attachment will be added to the collection. This is useful if the attachment is being added from a temporary file such as ETQB4914.TMP under meaningful name such as report.doc.
| Exception Type | Condition |
|---|---|
| MailBeeInvalidArgumentException | stream or targetFilename is a null reference (Nothing in Visual Basic). |
| MailBeeStreamException | An error occurred and ThrowExceptions is true. |
This sample opens a file stream, reads the file into the stream, adds the attachment from the stream, and saves the message to disk.
Note In real-world apps, it's easier to use Add overload to add an attachment from a file.
[C#] // To use the code below, import these namespaces at the top of your code. using System.IO; using MailBee; using MailBee.Mime; // The actual code (put it into a method of your class) // Open the file as stream for reading. using (FileStream fs = new FileStream(@"C:\Docs\1.gif", FileMode.Open)) { // Create new MailMessage object. MailMessage msg = new MailMessage(); // Set plain text body of the message. msg.BodyPlainText = "Hello, World!"; // Add the attachment to the message. msg.Attachments.Add(fs, "pic1.gif", "<12s4a8a8778c$5664i1b1$ir671781@tlffmdqjobxj>", "image/gif", null, NewAttachmentOptions.None, MailTransferEncoding.Base64); // Save message to disk. msg.SaveMessage(@"C:\SavedMessage.eml"); }
[Visual Basic] ' To use the code below, import these namespaces at the top of your code. Imports System.IO Imports MailBee Imports MailBee.Mime ' The actual code (put it into a method of your class) ' Open the file as stream for reading. Dim fs As FileStream fs = Nothing Try fs = New FileStream("C:\Docs\1.gif", FileMode.Open) ' Create new MailMessage object. Dim msg As New MailMessage ' Set plain text body of the message. msg.BodyPlainText = "Hello, World!" ' Add the attachment to the message. msg.Attachments.Add(fs, "pic1.gif", "<12s4a8a8778c$5664i1b1$ir671781@tlffmdqjobxj>", "image/gif", Nothing, NewAttachmentOptions.None, MailTransferEncoding.None) ' Save message to disk. msg.SaveMessage("C:\SavedMessage.eml") Finally If Not fs Is Nothing Then fs.Close() End Try
AttachmentCollection Class | MailBee.Mime Namespace | AttachmentCollection.Add Overload List | Attachment