в

ASP.NET

Всичко за платформата, която познавате и използвате

Как да изпращам email от кода на страницата

Последни съобщения 04-01-2010 9:24 от admin. 2 отговор (а).
Page 1 of 1 (3 items)
Sort Posts: Предишен Следващ
  • 02-19-2010 15:57

    Как да изпращам email от кода на страницата

     

    Свързано с:
    • 77.78.3.8
  • 02-19-2010 16:00 В отговор на

    • admin
    • Top 10 Contributor
    • Joined on 11-24-2008
    • Posts 35

    Re: Как да изпращам email от кода на страницата

    Public Class WebMailBGEncoding
            Public [To] As String
            Public Subject As String
            Public Body As String

            Public Sub Send()
                If [To] = "" Then
                    LogMassage = "Моля, въведете Вашия e-mail!"
                Else
                    If Subject = "" Then
                        LogMassage = "Моля, въведете тема!"
                    Else
                        If Body = "" Then
                            LogMassage = "Моля, въведете текст за тялото на e-mail!"
                        Else
                            ' System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
                            ' System.Net.Mail.SmtpClient is the alternate class for this in 2.0
                            Dim smtpClient As SmtpClient = New SmtpClient()
                            Dim message As New MailMessage
                            message.BodyEncoding = Encoding.UTF8
                            Try

                                Dim fromAddress As MailAddress = New MailAddress(FromEmail, FromName, Encoding.UTF8)

                                ' You can specify the host name or ipaddress of your server
                                ' Default in IIS will be localhost
                                smtpClient.Host = "localhost"

                                'Default port will be 25
                                smtpClient.Port = 25

                                'From address will be given as a MailAddress Object
                                message.From = "yourmail"

                                ' To address collection of MailAddress
                                message.To.Add([To])
                                message.Subject = Subject

                                ' CC and BCC optional
                                ' MailAddressCollection class is used to send the email to various users
                                ' You can specify Address as new MailAddress("admin1@yoursite.com")
                                'message.CC.Add("admin1@yoursite.com")

                                ' You can specify Address directly as string
                                '   message.Bcc.Add(New MailAddress(Bcc))

                                'Body can be Html or text format
                                'Specify true if it  is html message
                                message.IsBodyHtml = True

                                ' Message body content
                                message.Body = Body

                                ' Send SMTP mail
                                smtpClient.Send(message)

                            Catch ex As Exception
                                LogMassage = "Имаше проблем при изпращането на вашия email. Опитайте отново!"
                                'Catch ex As Exception
                                LogMassageDetails = CStr(Now()) + ", " + (("Следната грешка беше открита: " + ex.ToString()))
                                WriteAddRow(LogMassageDetails)
                                'check the InnerException
                                While Not (ex.InnerException Is Nothing)
                                    LogMassageDetails += (("<br/>Вътрешна грешка беше открита: " + ex.InnerException.ToString()))
                                    ex = ex.InnerException
                                End While
                            End Try
                        End If
                    End If
                End If

            End Sub

        End Class

    • 77.78.3.8
  • 04-01-2010 9:24 В отговор на

    • admin
    • Top 10 Contributor
    • Joined on 11-24-2008
    • Posts 35

    Re: Как да изпращам email от кода на страницата

    Ако Вашият майл сървър е защитен и изисква автентикация за да можете да изпращате имейли използвайте този код. 
    Той е от вида mail.yourdomain.com. Порта е 25.
    Задължително условие е този имейл от който ще изпращате да съществува във Вашия мейл сървър. Направено е с цел защита му от нежелано изпращане на СПАМ от други, чужди домейна и програми.

    Пример

    Web.config

      <system.net>
        <mailSettings>
          <smtp>
            <network
                 host="mail.yourdomain.com"
                 port="25"
                 userName="yourmail@yourdomain.com"
                 password="password" />
          </smtp>
        </mailSettings>
      </system.net>

    Code
        dim FromAddress As String ="name@yourdomain.com
        dim ToAddress As String ="name@anydomain.com

        'Create the MailMessage instance
        Dim MyMailMessage As New MailMessage(FromAddress, ToAddress)

        ' Assign the MailMessage's properties
        MyMailMessage.Subject = Subject.Text
        MyMailMessage.Body = Body.Text
        MyMailMessage.IsBodyHtml = False

        ' Create the SmtpClient object
        Dim smtp As New SmtpClient

        ' Send the MailMessage (will use the Web.config settings)
        smtp.Send(MyMailMessage)

    Свързано с:
    • 77.78.3.8
Page 1 of 1 (3 items)
Copyright ASPbg.NET, Powered by ASPhostBG
Powered by Community Server (Non-Commercial Edition), by Telligent Systems