E-mail problem

R

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
 
B

Bernie Yaeger

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
 
P

Peter Proost

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

hth Peter
 
M

Michel van den Berg

Could you verify that your smtp server is available? Try using the smtp
server without .net and see if it works.
 
R

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
 
B

Bernie Yaeger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top