By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,117 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,117 IT Pros & Developers. It's quick & easy.

E-mail problem

P: n/a
RD
In the following code, wen I get to SmtpMail.Send(myMessage) I get an
exception cast is invalid. I single step through the code and everuthing is
Ok till I execute that line.
Can anyone tell me whats wrong? I'm testing this on a Win XP Pro SP2. I get
the same error whether I specify a remote mail server like
"mail.myserver.mydomain.com" or if I specify local. On the local machine I
have IIS installed as well as Outlook 2003 and my mails with other progs all
work fine.

Here's the code snippet,

Thanks for any help,
Bob
Public Sub SendMailMultipleAttachments(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFiles As ArrayList = Nothing, _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "", _
Optional ByVal bTraceProgramExec As Boolean = False)

Dim myMessage As System.Web.Mail.MailMessage
Dim i, iCnt As Integer

Try
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
myMessage = New MailMessage
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer

If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next

End If

End With

If SMTPServer <> "" Then
SmtpMail.SmtpServer = SMTPServer
Else
SmtpMail.SmtpServer = "local"
End If
SmtpMail.Send(myMessage)
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
Catch ex As Exception
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
Error:" & ex.Message & " Stack Trace : " & ex.StackTrace)
End Try
End Sub
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi RD,

A couple of ideas.

1. remove the attachment and then send. See if this still fails.
2. set the smtpserver thus:
SmtpMail.SmtpServer = "127.0.0.1"

3. set the smtpmail.smtpserver to an smtp server that you know is trouble
free

4. attach the attachments as a string, thus:

Dim astring2 As String = "c:\my documents\visual basic notes.doc"
'msg.Attachments.Add(New MailAttachment(astring2))

or, from a server,

Dim astring3 As String = "\\imcsql\data\imcapps\sqldiag.txt"

msg.Attachments.Add(New MailAttachment(astring3))

HTH,

Bernie Yaeger

"RD" <no****@nospam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
In the following code, wen I get to SmtpMail.Send(myMessage) I get an
exception cast is invalid. I single step through the code and everuthing
is
Ok till I execute that line.
Can anyone tell me whats wrong? I'm testing this on a Win XP Pro SP2. I
get
the same error whether I specify a remote mail server like
"mail.myserver.mydomain.com" or if I specify local. On the local machine I
have IIS installed as well as Outlook 2003 and my mails with other progs
all
work fine.

Here's the code snippet,

Thanks for any help,
Bob
Public Sub SendMailMultipleAttachments(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFiles As ArrayList = Nothing, _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "", _
Optional ByVal bTraceProgramExec As Boolean = False)

Dim myMessage As System.Web.Mail.MailMessage
Dim i, iCnt As Integer

Try
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
myMessage = New MailMessage
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer

If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next

End If

End With

If SMTPServer <> "" Then
SmtpMail.SmtpServer = SMTPServer
Else
SmtpMail.SmtpServer = "local"
End If
SmtpMail.Send(myMessage)
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
Catch ex As Exception
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
Error:" & ex.Message & " Stack Trace : " & ex.StackTrace)
End Try
End Sub

Nov 21 '05 #2

P: n/a
Have you got option strict on? This helps a lot for cast is invalid errors
and a lot of other stuff.

hth Peter

"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:Op**************@TK2MSFTNGP10.phx.gbl...
Hi RD,

A couple of ideas.

1. remove the attachment and then send. See if this still fails.
2. set the smtpserver thus:
SmtpMail.SmtpServer = "127.0.0.1"

3. set the smtpmail.smtpserver to an smtp server that you know is trouble
free

4. attach the attachments as a string, thus:

Dim astring2 As String = "c:\my documents\visual basic notes.doc"
'msg.Attachments.Add(New MailAttachment(astring2))

or, from a server,

Dim astring3 As String = "\\imcsql\data\imcapps\sqldiag.txt"

msg.Attachments.Add(New MailAttachment(astring3))

HTH,

Bernie Yaeger

"RD" <no****@nospam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
In the following code, wen I get to SmtpMail.Send(myMessage) I get an
exception cast is invalid. I single step through the code and everuthing
is
Ok till I execute that line.
Can anyone tell me whats wrong? I'm testing this on a Win XP Pro SP2. I
get
the same error whether I specify a remote mail server like
"mail.myserver.mydomain.com" or if I specify local. On the local machine I have IIS installed as well as Outlook 2003 and my mails with other progs
all
work fine.

Here's the code snippet,

Thanks for any help,
Bob
Public Sub SendMailMultipleAttachments(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFiles As ArrayList = Nothing, _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "", _
Optional ByVal bTraceProgramExec As Boolean = False)

Dim myMessage As System.Web.Mail.MailMessage
Dim i, iCnt As Integer

Try
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments start " & Now())
myMessage = New MailMessage
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer

If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next

End If

End With

If SMTPServer <> "" Then
SmtpMail.SmtpServer = SMTPServer
Else
SmtpMail.SmtpServer = "local"
End If
SmtpMail.Send(myMessage)
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments start " & Now())
Catch ex As Exception
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments Error:" & ex.Message & " Stack Trace : " & ex.StackTrace)
End Try
End Sub


Nov 21 '05 #3

P: n/a
Could you verify that your smtp server is available? Try using the smtp
server without .net and see if it works.

RD wrote:
In the following code, wen I get to SmtpMail.Send(myMessage) I get an
exception cast is invalid. I single step through the code and everuthing is
Ok till I execute that line.
Can anyone tell me whats wrong? I'm testing this on a Win XP Pro SP2. I get
the same error whether I specify a remote mail server like
"mail.myserver.mydomain.com" or if I specify local. On the local machine I
have IIS installed as well as Outlook 2003 and my mails with other progs all
work fine.

Here's the code snippet,

Thanks for any help,
Bob
Public Sub SendMailMultipleAttachments(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFiles As ArrayList = Nothing, _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "", _
Optional ByVal bTraceProgramExec As Boolean = False)

Dim myMessage As System.Web.Mail.MailMessage
Dim i, iCnt As Integer

Try
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
myMessage = New MailMessage
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer

If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next

End If

End With

If SMTPServer <> "" Then
SmtpMail.SmtpServer = SMTPServer
Else
SmtpMail.SmtpServer = "local"
End If
SmtpMail.Send(myMessage)
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
Catch ex As Exception
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
Error:" & ex.Message & " Stack Trace : " & ex.StackTrace)
End Try
End Sub

Nov 21 '05 #4

P: n/a
RD
First thanks to all who replied
Problem is definitely related to trying to add attacments cause when i
cmment out my code that defines the attacments, the emails get sent.
This is the bit of code that seems to be creating problem, by the way option
strict is on.

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(Convert.ToString(AttachmentFiles(i)))
Then _
.Attachments.Add(AttachmentFiles(i).ToString)
Next
End If

AttachmentsFiles is an arraylist in which I previously put the strings of
the fully defined paths of the files to attach.
Commenting out this code, the messages get sent but of course with no
attachments, which is not exactly what I need.

Can anyone see what would be problem here?

Again thanks for help.

bob

"RD" <no****@nospam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
In the following code, wen I get to SmtpMail.Send(myMessage) I get an
exception cast is invalid. I single step through the code and everuthing is Ok till I execute that line.
Can anyone tell me whats wrong? I'm testing this on a Win XP Pro SP2. I get the same error whether I specify a remote mail server like
"mail.myserver.mydomain.com" or if I specify local. On the local machine I
have IIS installed as well as Outlook 2003 and my mails with other progs all work fine.

Here's the code snippet,

Thanks for any help,
Bob
Public Sub SendMailMultipleAttachments(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFiles As ArrayList = Nothing, _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "", _
Optional ByVal bTraceProgramExec As Boolean = False)

Dim myMessage As System.Web.Mail.MailMessage
Dim i, iCnt As Integer

Try
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
myMessage = New MailMessage
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer

If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next

End If

End With

If SMTPServer <> "" Then
SmtpMail.SmtpServer = SMTPServer
Else
SmtpMail.SmtpServer = "local"
End If
SmtpMail.Send(myMessage)
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
start " & Now())
Catch ex As Exception
If bTraceProgramExec Then MyTrace("SendMailMultipleAttachments
Error:" & ex.Message & " Stack Trace : " & ex.StackTrace)
End Try
End Sub

Nov 21 '05 #5

P: n/a
Hi Bob,

Try passing each to a string variable first, inside your for loop:
dim astring as string
for....
astring = attachmentfiles(i).tostring (or perhaps simply attachmentfiles(i)
..attachments.add(astring)
etc

HTH,

Bernie
"RD" <no****@nospam.net> wrote in message
news:O3**************@TK2MSFTNGP11.phx.gbl...
First thanks to all who replied
Problem is definitely related to trying to add attacments cause when i
cmment out my code that defines the attacments, the emails get sent.
This is the bit of code that seems to be creating problem, by the way
option
strict is on.

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(Convert.ToString(AttachmentFiles(i)))
Then _
.Attachments.Add(AttachmentFiles(i).ToString)
Next
End If

AttachmentsFiles is an arraylist in which I previously put the strings of
the fully defined paths of the files to attach.
Commenting out this code, the messages get sent but of course with no
attachments, which is not exactly what I need.

Can anyone see what would be problem here?

Again thanks for help.

bob

"RD" <no****@nospam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
In the following code, wen I get to SmtpMail.Send(myMessage) I get an
exception cast is invalid. I single step through the code and everuthing

is
Ok till I execute that line.
Can anyone tell me whats wrong? I'm testing this on a Win XP Pro SP2. I

get
the same error whether I specify a remote mail server like
"mail.myserver.mydomain.com" or if I specify local. On the local machine
I
have IIS installed as well as Outlook 2003 and my mails with other progs

all
work fine.

Here's the code snippet,

Thanks for any help,
Bob
Public Sub SendMailMultipleAttachments(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFiles As ArrayList = Nothing, _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "", _
Optional ByVal bTraceProgramExec As Boolean = False)

Dim myMessage As System.Web.Mail.MailMessage
Dim i, iCnt As Integer

Try
If bTraceProgramExec Then
MyTrace("SendMailMultipleAttachments
start " & Now())
myMessage = New MailMessage
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer

If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""

If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next

End If

End With

If SMTPServer <> "" Then
SmtpMail.SmtpServer = SMTPServer
Else
SmtpMail.SmtpServer = "local"
End If
SmtpMail.Send(myMessage)
If bTraceProgramExec Then
MyTrace("SendMailMultipleAttachments
start " & Now())
Catch ex As Exception
If bTraceProgramExec Then
MyTrace("SendMailMultipleAttachments
Error:" & ex.Message & " Stack Trace : " & ex.StackTrace)
End Try
End Sub


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.