How do I send an e-mail w/an attachment?

quahog

Well-known member
Joined
Jul 1, 2003
Messages
69
Location
dc
Hello all,

I have a quick question. How do I send an e-mail with and attachment? I am creating a reporting engine which sends reports via e-mail. I know there are more glorious ways to do. Currently, I send the email out with the report embedded as a csv file in the message. I want to attach the report instead.

Most of the code I am using is below.


oMsg.TextBody = "This is my message"
oMsg.Subject = "This is my title"
oMsg.From = "xxxx@xxx.com"
oMsg.To = "yyyy@xxx.com"

oMsg.Send()
oMsg = Nothing
iconfg = Nothing
ofields = Nothing
ofield = Nothing


I can send an e-mail. No problem. I have not been able to figure out how to send the attachment. I have been messing around with variations of the code listed below:

oMsg.Attachments.Add.FileName = "c:\file.xls"

Any help that you can provide would be greatly appreciated.

Thank you

Quahog
PHP:
[CODE]
 
Did you try using the MailAttachement object?
Code:
Dim attch As New Web.Mail.MailAttachment("Filename")
msg.Attachements.Add(attch)
 
Mutant,

Thanks for the quick response.

I tried using your suggestion.

My code references interop.cdo, which I should have told you to start with. I have placed more of my code below so you see what I am doing. I am using your code inside my existing code which has a reference to interop.cdo.

===>I have also included the errors that I recieved, after the arrow.

I grabbed this code from an old KB article which is not available any longer.

*******************************************
Dim oMsg As CDO.Message = New CDO.Message()
Dim iconfg As CDO.Configuration
Dim ofields As ADODB.Fields
Dim ofield As ADODB.Field
iconfg = oMsg.Configuration
ofields = iconfg.Fields
ofield = ofields("http://schemas.microsoft.com/cdo/configuration/sendusing")
ofield.Value = 2
*************************
Dim attch As New Web.Mail.MailAttachment("Filename")

=======>Error: type web.mail.attachment is not defined.
oMsg.Attachements.Add(attch)

=======>Error: Attachments is not a member of cdo.message
******************************
ofield = ofields("http://schemas.microsoft.com/cdo/configuration/smtpserver")
ofield.Value = "mail server name"
ofields.Update()
oMsg.Configuration = iconfg
oMsg.TextBody = "This is my message"
oMsg.Subject = "This is my title"
oMsg.From = "xxxx@xxx.com"
oMsg.To = "yyyy@xxx.com"
oMsg.Send()
oMsg = Nothing
iconfg = Nothing
ofields = Nothing
ofield = Nothing
 
Mutant or anyone else who wants to help.

If you have a better way of sending the mail, I would appreciate it. I am not stuck on the method that I have listed above.
 
Add a reference to System.Web.dll
Code:
Imports Web.Mail

Dim Msg As MailMessage = New MailMessage()
Msg.To = "Someone@Somewhere.com"
Msg.Body = "Hello" & Environment.Newline & "Goodbye"
Msg.From = "Me@here.com"
Msg.Subject = "Things"
Msg.Attachments.Add(New MailAttachment("C:\MyFile.txt"))
Msg.Attachments.Add(New MailAttachment("C:\Windows\Clouds.bmp"))

SmtpMail.SmtpServer = "mail.somewhere.com" Im not exactly sure how this works
SmtpMail.Send(Msg)
 
Last edited by a moderator:
The web route works.

I added the reference and used the code that you suggested and it works.

Thank you, gentleman.
 
Back
Top