quahog Posted September 19, 2003 Posted September 19, 2003 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] Quote when the day is bad and life's a curse, cheer up tomorrow may be worse.
*Experts* mutant Posted September 19, 2003 *Experts* Posted September 19, 2003 Did you try using the MailAttachement object? Dim attch As New Web.Mail.MailAttachment("Filename") msg.Attachements.Add(attch) Quote
quahog Posted September 19, 2003 Author Posted September 19, 2003 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 Quote when the day is bad and life's a curse, cheer up tomorrow may be worse.
quahog Posted September 19, 2003 Author Posted September 19, 2003 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. Quote when the day is bad and life's a curse, cheer up tomorrow may be worse.
AndreRyan Posted September 20, 2003 Posted September 20, 2003 (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 September 20, 2003 by AndreRyan Quote .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?
quahog Posted September 22, 2003 Author Posted September 22, 2003 The web route works. I added the reference and used the code that you suggested and it works. Thank you, gentleman. Quote when the day is bad and life's a curse, cheer up tomorrow may be worse.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.