Send Email using VB.NEt 2005

G

Guest

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 = "(e-mail address removed)"
SendTo = “[email protected]â€
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
 
I

Izzy

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 = "(e-mail address removed)"
SendTo = "(e-mail address removed)"
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?
 
G

Guest

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.
 
I

Izzy

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.






- 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.
 

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