Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

[code]
when the day is bad and life's a curse, cheer up tomorrow may be worse.
Posted

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

when the day is bad and life's a curse, cheer up tomorrow may be worse.
Posted

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.

when the day is bad and life's a curse, cheer up tomorrow may be worse.
Posted (edited)

Add a reference to System.Web.dll

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" 'I'm not exactly sure how this works
SmtpMail.Send(Msg)

Edited by AndreRyan
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted

The web route works.

 

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

 

Thank you, gentleman.

when the day is bad and life's a curse, cheer up tomorrow may be worse.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...