Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, guys thanks for time and help..

i have a windows services in vb.net for send emails using any host
like yahoo, gmail, internal exchange.

i have one issue my pc is in a network using isa server as proxy

when i try to send the email from windows services i get
"Unable to connect to the remote host"

but if i test the function for send email in a windows form i can send the email

what i have to check on my windows services?

this is my code:


VB
<pre>   Public Function EmailService(ByVal Port As String, ByVal Host As String, ByVal FromEmail As String, ByVal ToEmail As String, ByVal Subject As String, ByVal Body As String, Optional EmailAtt As String = "")
        Try
            Dim SmtpServer As New SmtpClient()
            Dim key As Long
            key = "****"
            If Is64Bit() Then
                regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Test", False)
            Else
                regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Test", False)
            End If

            Dim mysqlconn As MySqlConnection
            Dim mysqlcmd As MySqlCommand = New MySqlCommand
            Dim rd As MySqlDataReader

            Dim sqlEmailUsr As String = "SELECT ParamValue FROM userparam WHERE ParamName = 'UserEmail'"
            Dim sqlEmailPass As String = "SELECT ParamValue FROM userparam = 'UserEmailPassword'"
            Dim Email_User As String = ""
            Dim Email_Pass As String = ""

            mysqlconn = New MySqlConnection()
            mysqlconn.ConnectionString = readdbfile()

            If mysqlconn.State = Data.ConnectionState.Open Then

            Else
                mysqlconn.Open()
            End If

            Dim cmd As MySqlCommand = mysqlconn.CreateCommand()

            cmd.CommandText = sqlEmailUsr
            rd = cmd.ExecuteReader
            rd.Read()
            Email_User = rd.GetString(0).ToString
            rd.Close()

            cmd.CommandText = sqlEmailPass
            rd = cmd.ExecuteReader
            rd.Read()
            Email_Pass = rd.GetString(0).ToString
            rd.Close()

            SmtpServer.UseDefaultCredentials = False
            Dim Auth As New System.Net.NetworkCredential(Email_User, Decrypting(Email_Pass, key))
            SmtpServer.Port = Port
            SmtpServer.Host = Host
            If regKey.GetValue("Email_SSL") = "1" Then SmtpServer.EnableSsl = True
            If regKey.GetValue("Email_Auth") = "1" Then SmtpServer.Credentials = Auth
            Dim MailRecip() As String = Nothing
            Dim attachment As System.Net.Mail.Attachment
            attachment = New System.Net.Mail.Attachment(EmailAtt)
            If Trim(ToEmail.ToString) <> "" Then MailRecip = Split(Trim(ToEmail.ToString), ";")
            Dim mail As New MailMessage(New MailAddress(FromEmail.Trim()), New MailAddress(MailRecip(0)))
            If MailRecip.Length > 1 Then
                For x As Integer = 1 To MailRecip.Length - 1
                    mail.Bcc.Add(MailRecip(x))
                Next
            End If
            mail.Subject = (Subject)
            mail.Body = (Body)
            mail.IsBodyHtml = True
            mail.Priority = MailPriority.High
            mail.Attachments.Add(attachment)
            mail.ReplyToList.Add(FromEmail)
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess Or DeliveryNotificationOptions.OnFailure Or DeliveryNotificationOptions.Delay
            mail.Headers.Add("Read-Receipt-To", FromEmail)
            mail.Headers.Add("Disposition-Notification-To", FromEmail)
            SmtpServer.Send(mail)
            mysqlconn.Close()
        Catch ex As SmtpException
            WriteEventLogError("An error occurred Email Service: " & ex.InnerException.Message, 2000)
        End Try
        Return Nothing
    End Function


on windows form i test this function and i can send email using smtp.gmail.com and yahoo or hotmail.

but in the windows services is not working only i can send email to my corporate network
Posted
Updated 16-Jul-14 15:13pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900