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

Send Email using VB.NEt 2005

P: n/a
Hi There,

I use follow code to send email inside VB.NET 2005. It does not work well.
Error message of "Failure sending email" would occue. However, email was
sent out sometimes. I am confused and please help.

Thanks in advance.

Hugh

Imports System.net.Mail

Public Class Form1

Sub SendEmail(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject As String, ByVal strMessage _
As String, ByVal file As String)

Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New
MailAddress(strTo))
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True

If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If

'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Catch ex As Exception
MessageBox.Show(ex.Message & " in SendEmail")
End Try
End Sub

Sub SendOneEmail()
Dim SendFrom, SendTo, Attach, strSubject, strMessage As String

Try
SendFrom = "ab***@yahoo.com"
SendTo = “fg***@yahoo.com”
Attach = "c:\text.txt"
strSubject = “Email Subject”
strMessage = “Email Body. This is a test”
SendEmail(SendFrom, SendTo, strSubject, strMessage, Attach)
Catch ex As Exception
MessageBox.Show(ex.Message & "in SendOneEmail")
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SendOneEmail()
End Sub

End Class
Feb 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 15, 11:08 am, Hugh <H...@discussions.microsoft.comwrote:
Hi There,

I use follow code to send email inside VB.NET 2005. It does not work well.
Error message of "Failure sending email" would occue. However, email was
sent out sometimes. I am confused and please help.

Thanks in advance.

Hugh

Imports System.net.Mail

Public Class Form1

Sub SendEmail(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject As String, ByVal strMessage _
As String, ByVal file As String)

Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New
MailAddress(strTo))
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True

If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If

'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Catch ex As Exception
MessageBox.Show(ex.Message & " in SendEmail")
End Try
End Sub

Sub SendOneEmail()
Dim SendFrom, SendTo, Attach, strSubject, strMessage As String

Try
SendFrom = "a...@yahoo.com"
SendTo = "f...@yahoo.com"
Attach = "c:\text.txt"
strSubject = "Email Subject"
strMessage = "Email Body. This is a test"
SendEmail(SendFrom, SendTo, strSubject, strMessage, Attach)
Catch ex As Exception
MessageBox.Show(ex.Message & "in SendOneEmail")
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SendOneEmail()
End Sub

End Class
Is the .txt file in use when you try to send the email?

Feb 15 '07 #2

P: n/a
Hi Izzy,

I am not sure what you asked. I assume that you meant attachment file.
Yes, attachment was sent in the email. I did find the attachment in the
email that were able to sent out.

"Izzy" wrote:
On Feb 15, 11:08 am, Hugh <H...@discussions.microsoft.comwrote:
Hi There,

I use follow code to send email inside VB.NET 2005. It does not work well.
Error message of "Failure sending email" would occue. However, email was
sent out sometimes. I am confused and please help.

Thanks in advance.

Hugh

Imports System.net.Mail

Public Class Form1

Sub SendEmail(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject As String, ByVal strMessage _
As String, ByVal file As String)

Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New
MailAddress(strTo))
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True

If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If

'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Catch ex As Exception
MessageBox.Show(ex.Message & " in SendEmail")
End Try
End Sub

Sub SendOneEmail()
Dim SendFrom, SendTo, Attach, strSubject, strMessage As String

Try
SendFrom = "a...@yahoo.com"
SendTo = "f...@yahoo.com"
Attach = "c:\text.txt"
strSubject = "Email Subject"
strMessage = "Email Body. This is a test"
SendEmail(SendFrom, SendTo, strSubject, strMessage, Attach)
Catch ex As Exception
MessageBox.Show(ex.Message & "in SendOneEmail")
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SendOneEmail()
End Sub

End Class

Is the .txt file in use when you try to send the email?

Feb 15 '07 #3

P: n/a
On Feb 15, 11:56 am, Hugh <H...@discussions.microsoft.comwrote:
Hi Izzy,

I am not sure what you asked. I assume that you meant attachment file.
Yes, attachment was sent in the email. I did find the attachment in the
email that were able to sent out.

"Izzy" wrote:
On Feb 15, 11:08 am, Hugh <H...@discussions.microsoft.comwrote:
Hi There,
I use follow code to send email inside VB.NET 2005. It does not work well.
Error message of "Failure sending email" would occue. However, email was
sent out sometimes. I am confused and please help.
Thanks in advance.
Hugh
Imports System.net.Mail
Public Class Form1
Sub SendEmail(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject As String, ByVal strMessage _
As String, ByVal file As String)
Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New
MailAddress(strTo))
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If
'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Catch ex As Exception
MessageBox.Show(ex.Message & " in SendEmail")
End Try
End Sub
Sub SendOneEmail()
Dim SendFrom, SendTo, Attach, strSubject, strMessage As String
Try
SendFrom = "a...@yahoo.com"
SendTo = "f...@yahoo.com"
Attach = "c:\text.txt"
strSubject = "Email Subject"
strMessage = "Email Body. This is a test"
SendEmail(SendFrom, SendTo, strSubject, strMessage, Attach)
Catch ex As Exception
MessageBox.Show(ex.Message & "in SendOneEmail")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SendOneEmail()
End Sub
End Class
Is the .txt file in use when you try to send the email?- Hide quoted text -

- Show quoted text -
What I'm saying is....if this code works sometimes and not others,
that might be because the attachment file "whatever.txt" might be in
use by another process when you attempt to send the email.
Is there another process which creates this attachment file? If so it
might not be releasing it quick enough. I've run into this problem
before when creating .pdf files. My solution was to check the creation
date of the file and only use files older then 10 seconds, 5 might
even be ok.
Feb 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.