Friday, December 23, 2011

M_Email Module V-1


Imports System.Net.Mail
Imports System.Xml

Module M_E_Mails
    Public mail As New MailMessage()

    Public Pb_Smtp_Svr As String
    Public Pb_Pop3_Svr As String
    Public Pb_Smtp_Port As String
    Public Pb_Pop3_Port As String
    Public Pb_Email_Id As String
    Public Pb_Password As String
    Public Pb_Smtp_SSL As String
    Public Pb_Pop3_SSL As String
    Public Pb_User_Name As String

    Public Class Get_Email_Config
        Property P_Smtp_Svr As String
        Property P_Pop3_Svr As String
        Property P_Smtp_Port As String
        Property P_Pop3_Port As String
        Property P_Email_Id As String
        Property P_Password As String
        Property P_Smtp_SSL As String
        Property P_Pop3_SSL As String
        Property P_User_Name As String
    End Class

    Public Function Load_Mail_Settings() As Get_Email_Config
        Read_Mail_Settings()
        Dim My_Config As New Get_Email_Config

        My_Config.P_Smtp_Svr = Pb_Smtp_Svr
        My_Config.P_Pop3_Svr = Pb_Pop3_Svr
        My_Config.P_Smtp_Port = Pb_Smtp_Port
        My_Config.P_Pop3_Port = Pb_Pop3_Port
        My_Config.P_Email_Id = Pb_Email_Id
        My_Config.P_Password = Pb_Password
        My_Config.P_Smtp_SSL = Pb_Smtp_SSL
        My_Config.P_Pop3_SSL = Pb_Pop3_SSL
        My_Config.P_User_Name = Pb_User_Name
        Return My_Config
    End Function

    Public Sub Read_Mail_Settings()

        Dim xDoc As XmlDocument = New XmlDocument()
        xDoc.Load("Config_Email.xml")

        Dim E_Mail_ID As XmlNodeList = xDoc.GetElementsByTagName("E_ID")
        Dim Smtp_Svr As XmlNodeList = xDoc.GetElementsByTagName("SMTP_Svr_Name")
        Dim Smtp_Port As XmlNodeList = xDoc.GetElementsByTagName("SMTP_Port")
        Dim Smtp_SSL As XmlNodeList = xDoc.GetElementsByTagName("SMTP_SSL")
        Dim POP3_Svr As XmlNodeList = xDoc.GetElementsByTagName("POP3_Svr_Name")
        Dim POP3_Port As XmlNodeList = xDoc.GetElementsByTagName("POP3_Port")
        Dim POP3_SSL As XmlNodeList = xDoc.GetElementsByTagName("POP3_SSL")
        Dim User_Name As XmlNodeList = xDoc.GetElementsByTagName("User_Name")
        Dim Password As XmlNodeList = xDoc.GetElementsByTagName("Password")

        Try
           
            Pb_Smtp_Svr = Smtp_Svr(0).InnerText
            Pb_Pop3_Svr = POP3_Svr(0).InnerText
            Pb_Smtp_Port = Smtp_Port(0).InnerText
            Pb_Pop3_Port = POP3_Port(0).InnerText
            Pb_Smtp_SSL = Smtp_SSL(0).InnerText
            Pb_Pop3_SSL = POP3_SSL(0).InnerText
            Pb_Email_Id = E_Mail_ID(0).InnerText
            Pb_Password = Password(0).InnerText
            Pb_User_Name = User_Name(0).InnerText

        Catch ex As Exception
            MsgBox("Read User Login XML....   " & ex.Message)
        End Try

    End Sub

    Public Sub Write_Mail_Settings(ByVal P_Smtp_Svr As String, ByVal P_Pop3_Svr As String, ByVal P_Smtp_Port As String, _
                                   ByVal P_Pop3_Port As String, ByVal P_Smtp_SSL As String, ByVal P_Pop3_SSL As String, _
                                   ByVal P_Email_Id As String, ByVal P_Password As String, ByVal P_User_Name As String)

        Dim xDoc As XmlDocument = New XmlDocument()

        xDoc.Load("Config_Email.xml")

        Dim E_Mail_ID As XmlNodeList = xDoc.GetElementsByTagName("E_ID")
        Dim Smtp_Svr As XmlNodeList = xDoc.GetElementsByTagName("SMTP_Svr_Name")
        Dim Smtp_Port As XmlNodeList = xDoc.GetElementsByTagName("SMTP_Port")
        Dim Smtp_SSL As XmlNodeList = xDoc.GetElementsByTagName("SMTP_SSL")
        Dim POP3_Svr As XmlNodeList = xDoc.GetElementsByTagName("POP3_Svr_Name")
        Dim POP3_Port As XmlNodeList = xDoc.GetElementsByTagName("POP3_Port")
        Dim POP3_SSL As XmlNodeList = xDoc.GetElementsByTagName("POP3_SSL")
        Dim User_Name As XmlNodeList = xDoc.GetElementsByTagName("User_Name")
        Dim Password As XmlNodeList = xDoc.GetElementsByTagName("Password")

        Try

            Smtp_Svr(0).InnerText = P_Smtp_Svr
            POP3_Svr(0).InnerText = P_Pop3_Svr
            Smtp_Port(0).InnerText = P_Smtp_Port
            POP3_Port(0).InnerText = P_Pop3_Port
            Smtp_SSL(0).InnerText = P_Smtp_SSL
            POP3_SSL(0).InnerText = P_Pop3_SSL
            E_Mail_ID(0).InnerText = P_Email_Id
            Password(0).InnerText = P_Password
            User_Name(0).InnerText = P_User_Name

        Catch ex As Exception
            MsgBox("Read User Login XML....   " & ex.Message)
        End Try

        xDoc.Save("Config_Email.xml")
    End Sub

    Public Sub SendEmail(ByVal ToAddress As String, ByVal Mail_Subject As String, ByVal Mail_body As String)
        Read_Mail_Settings()

        Dim client As New SmtpClient()
        Dim sendTo As New MailAddress(ToAddress)
        Dim from As MailAddress = New MailAddress(Pb_Email_Id)
        Dim message As New MailMessage(from, sendTo)


        message.IsBodyHtml = True
        message.Subject = Mail_Subject
        message.Body = Mail_body

        ' Use the same account in app.config to authenticate.
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential(Pb_Email_Id, Pb_Password)


        client.Host = "smtp.gmail.com"
        client.UseDefaultCredentials = False
        client.Credentials = basicAuthenticationInfo
        client.EnableSsl = True
        client.Port = 587

        Try

            client.Send(message)
            MsgBox("SUCCESS")

            client.Send(message)
            MsgBox("SUCCESS")

            client.Send(message)
            MsgBox("SUCCESS")

        Catch ex As Exception

            MsgBox("SEND FAIL")

        End Try
        ' 'mail = New MailMessage()
        '  Dim addr() As String = TextBox1.Text.Split(",")

        'Try
        'mail.From = New MailAddress("xyz@gmail.com",
        '   "Web Developers", System.Text.Encoding.UTF8)

        'Dim i As Byte
        'For i = 0 To addr.Length - 1
        'mail.To.Add(addr(i))
        'Next
        'mail.Subject = TextBox3.Text
        'mail.Body = TextBox4.Text
        'If ListBox1.Items.Count <> 0 Then
        'For i = 0 To ListBox1.Items.Count - 1
        '           mail.Attachments.Add(New Attachment
        '              (ListBox1.Items.Item(i)))
        'Next
        'End If
        'mail.DeliveryNotificationOptions =
        'DeliveryNotificationOptions.OnFailure()
        'mail.ReplyTo = New MailAddress(TextBox1.Text)
        'SmtpServer.Send(mail)
        'Catch ex As Exception
        'MsgBox(ex.ToString())
        'End Try
    End Sub

End Module

No comments:

Post a Comment