Tuesday, 19 November 2013

Send email with WCF code on Windows Azure Web role


Azure doesn't support the SMTP service because if it did, it would quickly become the world's biggest spamming engine and the Azure IP address range would soon be blacklisted.
Relay through an on-premise or third-party SMTP relay service.
Writing a Service (WCF or WebAPI ) would be good idea reason being it can be accessed from various sources like classic ASP code, windows application and any regular web application. Hence making sure same code doesn't repeat in defferent applications.

This service can be part of a web role (common solution) or you can host separately with a worker role.

Email code in service  -

 Public Function SendEmail(ByVal emailFrom As String, ByVal sendTo As String, ByVal msgSubject As String, ByVal msgBody As String, ByVal ccString As String, ByVal bccString As String) As String _
    Implements ISendEmail.SendEmail

        Try
            Dim msg As New MailMessage()
            Dim fromMail As New MailAddress(emailFrom)

            'For Each address As String In sendTo
            '    msg.To.Add(New MailAddress(address))
            'Next

            msg.From = fromMail
            msg.To.Add(New MailAddress(sendTo))

            If Not String.IsNullOrEmpty(ccString) Then
                msg.CC.Add(New MailAddress(ccString))
            End If

            If Not String.IsNullOrEmpty(bccString) Then
                msg.Bcc.Add(New MailAddress(bccString))
            End If

            msg.Subject = msgSubject

            msg.Body = msgBody
            msg.IsBodyHtml = True

            Dim smtp As New SmtpClient(ConfigurationManager.AppSettings("SMTPServer"), ConfigurationManager.AppSettings("SMTPServerPort"))
            Dim credentials As New NetworkCredential(ConfigurationManager.AppSettings("SMTPUser"), ConfigurationManager.AppSettings("SMTPPassword"))


            smtp.Credentials = credentials
            smtp.EnableSsl = False

            smtp.Send(msg)
            msg.Dispose()

            Return "emailsent"

        Catch ex As Exception
            Return "error" + ex.Message
        End Try
    End Function

Once the code is compiled this can be deployed a VM / web role / worker role.

Classic ASP usig the SMTP email service-


    Function SendEmail(ByVal fromAddress, ByVal toAddress, ByVal subject, ByVal body, Byval ccString, byval bccString)

    Dim soapServer, soapMessage
    Dim xmlhttp

    soapServer = Application("UnsecureRoot") & "/EmailNotification/SendEmail.svc"

soapMessage = "<s:Envelope xmlns:s=" & GetQuotedUrl("http://schemas.xmlsoap.org/soap/envelope/") & ">" & _ 
"<s:Body>" & _ 
"<SendEmail xmlns=" & GetQuotedUrl("http://tempuri.org/") & "><emailFrom>" & fromAddress & "</emailFrom>" & _ 
                            "<sendTo>" & toAddress & "</sendTo>" & _ 
                            "<msgSubject>" & subject & "</msgSubject>" & _ 
                            "<msgBody>" & body & "</msgBody>" & _ 
                            "<ccString>" & ccString & "</ccString>" & _ 
                            "<bccString>" & bccString & "</bccString>" & _ 
                            "</SendEmail>" & _
"</s:Body>" & _
"</s:Envelope>"


soapMessage = Replace(soapMessage, "'", chr(34))
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
                       
                
xmlhttp.open "POST", soapServer, False
xmlhttp.setRequestHeader "Man", "POST" & " " & soapServer & " HTTP/1.1"
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/ISendEmail/SendEmail"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.send(soapMessage)

    SendEmail = xmlhttp.responseText
    
End Function

    Function GetQuotedUrl(ByVal value)
        GetQuotedUrl = Chr(34) & value & Chr(34)
    End Function


No comments:

Post a Comment