Jump to content

Sending Multiple Attachments in VB.NET


Guest Something_Snorkel

Recommended Posts

Guest Something_Snorkel

Hi everyone,

I'm having issues with sending multiple attachments as follows...

I have a FileSystemWatcher class, which 'OnCreate' handles the event two fold:

First it calls my sendMail class to send the mail message attaching file(s) found with FileSystemWatcher and then secondly moves the file(s) sent to a archive directory.

My problem is I am having difficulty sending the attachments (fileName as string) to my sendMail class sub-routine as type AttachmentCollection from my 'Public Shared OnCreate' sub-routine (seperate class). I'm not even sure if this is the best way of going about this? I know the code below should work, it's just getting my string values into an array list to push through Att as AttachmentCollection that is giving me grief.

My SendMail class sub-routine code is as follows:

Public Sub emailPush(ByVal Att As AttachmentCollection)

'Construct the email message
Dim mailMsg As New MailMessage(eFrom, eTo, eSubject, eBody)

For Each item As Attachment In Att
mailMsg.Attachments.Add(item)
Next

'Attempt to send the message
Try
Dim client As New SmtpClient(smtpServerName)
client.Send(mailMsg)

Catch exp As Exception
MessageBox.Show("The following problem occurred when attempting to " & _
"send your email: " & exp.Message, _
Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

mailMsg.Dispose()

End Sub

Any help is greatly appreciated.

Link to comment
Share on other sites


Guest Something_Snorkel

After more searching I found this helpful link on the net System Net Mail

It goes into some depth regarding the new namespace from the .NET framework (v2)

If you require information on the old namespace from .NET framework (v1) use this link System Web Mail

So this solved my issue of how to add multiple attachments. I didn't use the AttachmentCollection method in the end but I would still like to know how to add multiple attachments this way and how to use the 'createattachmentfromstring' method if anyone knows; I read the info under the same link (above) under the title 3.4 Working with Attachments, sub listing 3.4.2 How do I create an attachment from a stream? which was interesting but still didn't say anything about the 'createattachmentfromstring' so I'd like info on this if anyone knows.

Link to comment
Share on other sites

  • 2 years later...

See http://www.vbforums.com/showthread.php?t=502947 for sending an attachment from a URL. You can save a string to a textfile using a StreamWriter command, then take that file using the methods at that link and attach it (basically similar to what you saw at your link, just using an

string fileUrl = "http://localhost/mytextfile.txt";
string[] fileName = fileUrl.Split('/');
string file = fileName[fileName.Length - 1];

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();

// Use the file name found by splitting on the URL
Attachment data = new Attachment(stream, file, response.ContentType);

// Add the information for the file
ContentDisposition disposition = data.ContentDisposition;
disposition.FileName = file;
disposition.CreationDate = File.GetCreationTime(file);
disposition.ModificationDate = File.GetLastWriteTime(file);
disposition.ReadDate = File.GetLastAccessTime(file);

// Add the file attachment to this e-mail message.
message.Attachments.Add(data);

kind of method.)

You can't take a simple string and send it as an attachment without it becoming a file in-between, at some point. Just write your string/text to a text file. (Above code assumes anonymous authentication to the URL. Can also use Windows authentication by passing request.Credentials = CredentialCache.DefaultCredentials)

-Tom

Edited by navyjax2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...