473,413 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

E-mail problem

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
5 1267
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kari Setälä | last post by:
I'm using ASP VBScript. I have a feedback form that should do two things: insert the contents into a database and send the contents as email. The address it would be sent to varies - users select...
2
by: weiwei | last post by:
I have an ASP automatic email confirmation code on Windows 2003 Server. My problem is that the message can be submitted to only some email accounts such as hotmail or yahoo, but cannot to my office...
3
by: Andrew J | last post by:
Hi all Would really appreciate if someone can help mi with a problem I've been having with ASP generated emails. In fact, this might be a question more related to Outlook than ASP, but in any case...
4
by: number1.email | last post by:
I have tried the following code: FileName: index2.php --------------------------------- <? mail( "aaa@yahoo.com", "A", "B", "From: a...@aol.com" ); echo "Finished Emailing."; ?>
0
by: | last post by:
Dear all, I try to send email through coding....which is successful (can receive the mail) ...however,,,when i add a attachment...even no error...but not receive any mail....don't know what's...
1
by: Lenny | last post by:
I wrote this function to send an email the problem is that I do not receive any error from .net but the email is not delivered. smtp server is not local but from my ISP any suggestion on how to...
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi, I'm using the bellow code to send emails in a Website that I uploaded in one of my client's server where it has SMTP installed, BUT some users are receiving email some others aren't !!?So,...
3
crystal2005
by: crystal2005 | last post by:
I found such eror message when i tried to test sending email. Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in...
2
by: =?Utf-8?B?YmFyYnlxMTA=?= | last post by:
After sending mail, it leaves the outbox as normal and clears and hits the sent folder, but then I get a popup box that states EMAIL PROBLEM One or more e-mail messages cannot be sent. ...
11
by: phpmind | last post by:
I am attempting to send and email via my feedback form and nothing seems to go thru. I have tried sending to a yahoo account and a gmail account. I have changed a few things in my script several...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.