Envío de correo electrónico mediante WebDav


A propósito de envío de correo electrónico, he actualizado el artículo: Envío de correo electrónico por SMTP con autenticación con información para el .NET Framework 2.0.

Volviendo con WebDav, en el siguiente ejemplo la clase HttpWebRequest envía una petición a una computadora que tiene Microsoft Exchange 2000 Server, y la clase HttpWebResponse recibe la respuesta. Ambas clases se encuentran bajo el Namespace “System.Net”.

Imports System
Imports System.Net
Imports System.IO
  Module WebDavMail
    Sub Main()
      Try
        Dim strServer As String = "ExchServe"
        Dim strSenderAlias As String = "sender"
        Dim strFrom As String = "sender@example.com"
        Dim strTo As String = "recipient@example.com"
        Dim strSubject As String = "Envío de mensaje utilizando HttpWebRequest"
        Dim strBody As String = "Hola Mundo"
        Dim sUri As String
        sUri = "http://" & strServer & "/Exchange/" & strSenderAlias
        sUri = sUri & "/%23%23DavMailSubmissionURI%23%23/"
        Dim myUri As System.Uri = New System.Uri(sUri)
        Dim HttpWRequest As HttpWebRequest = _
CType(WebRequest.Create(myUri), HttpWebRequest)
Dim sQuery As String
Dim mySentTime As DateTime = New DateTime
sQuery = "From: " & strFrom & Environment.NewLine & _
"To: " & strTo & Environment.NewLine & _
"Subject: " & strSubject & Environment.NewLine & _
"Date: " & DateTime.Now.ToString & Environment.NewLine & _
"X-Mailer: My DAV mailer" & Environment.NewLine & _
"MIME-Version: 1.0" & Environment.NewLine & _
"Content-Type: text/plain;" & Environment.NewLine & _
"Charset = ""iso-8859-1""" & Environment.NewLine & _
"Content-Transfer-Encoding: 7bit" & Environment.NewLine & _
Environment.NewLine & strBody
Dim myCred As NetworkCredential = _
New NetworkCredential("DomainName\User", "Password")
Dim myCredentialCache As CredentialCache = New CredentialCache
myCredentialCache.Add(myUri, "Basic", myCred)
HttpWRequest.Credentials = myCredentialCache
HttpWRequest.Headers.Set("Translate", "f")
HttpWRequest.ContentType = "message/rfc822"
HttpWRequest.ContentLength = sQuery.Length
HttpWRequest.Timeout = 300000
HttpWRequest.Method = "PUT"
Dim ByteQuery As Byte() = System.Text.Encoding.ASCII.GetBytes(sQuery)
HttpWRequest.ContentLength = ByteQuery.Length
Dim QueryStream As Stream = HttpWRequest.GetRequestStream
QueryStream.Write(ByteQuery, 0, ByteQuery.Length)
QueryStream.Close
Dim HttpWResponse As HttpWebResponse = _
CType(HttpWRequest.GetResponse, HttpWebResponse)
Dim iStatCode As Integer = CType(HttpWResponse.StatusCode, Integer)
Dim sStatus As String = iStatCode.ToString
Console.WriteLine("Status Code: {0}", sStatus)
Dim sReqHeaders As String = HttpWRequest.Headers.ToString
Console.WriteLine(sReqHeaders)
Dim strm As Stream = HttpWResponse.GetResponseStream
Dim sr As StreamReader = New StreamReader(strm)
Dim sText As String = sr.ReadToEnd
Console.WriteLine("Response: {0}", sText)
strm.Close
myCred = Nothing
myCredentialCache = Nothing
HttpWRequest = Nothing
HttpWResponse = Nothing
QueryStream = Nothing
strm = Nothing
sr = Nothing
Catch e As Exception
Console.WriteLine("Excepción: {0}", e)
End Try
End Sub
End Module

Acerca de Willy Mejia

Developer, Techie, Human... http://about.me/willyxoft
Esta entrada fue publicada en .NET. Guarda el enlace permanente.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s