SPF compliant .net system.net.mail .mailmessage

As a follow up to my post SPF compliant CDO message, here is the equivalent (well, not quite, but similar) vb.net version that I use –

Public Sub Send(ByVal strTo As String, ByVal strFrom As String, _
   ByVal strSender As String, ByVal strCC As String, _
   ByVal strBCC As String, ByVal strSubject As String, _
   ByVal strBody As String)

Dim MailObj As New System.Net.Mail.MailMessage(strFrom, _
   strTo, strSubject, strBody)

'SPF Stuff
If Not String.IsNullOrEmpty(strSender) And strSender <> strFrom Then
   MailObj.Headers.Add("return-path", strSender)
   MailObj.Headers.Add("reply-to", strFrom)
   MailObj.Sender = New Net.Mail.MailAddress(strSender)
End If

If Not strCC = String.Empty Then
   For Each cc As String In strCC.Split(";")
      MailObj.CC.Add(cc)
   Next
End If

If Not strBCC = String.Empty Then
   For Each bcc As String In strBCC.Split(";")
      MailObj.Bcc.Add(bcc)
   Next
End If

Dim MailClient As New System.Net.Mail.SmtpClient
MailClient.Host = "mailserver"
MailClient.Send(MailObj)

End Sub

Again, as before, this is generally useful for web generated emails (like send a friend forms etc.), simply specify the users address as strFrom and a generic local address (noreply@mydomain.com) as strSender.

I hope it proves useful.

add to del.icio.us :: Bookmark Post in Technorati :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook :: Bookmark on Google :: Add to Netscape :: Share on Yahoo :: Add this to Live

SPF compliant CDO message

I wrote this a while back as I couldn’t find a nice example anywhere for sending SPF compliant CDO.message emails. It also copes with setting message importance which is another awkward thing. Let me know if you have any problems or have any suggestions to improve it.

Sub sendemail(strFrom, strTo, strSender, strBcc, _
   strSubject, strBody, intType, intImportance)
Dim objMessage ' As CDO.Message
objMessage = Server.CreateObject("CDO.Message")
With objMessage
   .Fields("urn:schemas:httpmail:importance").Value = intImportance
   .Fields("urn:schemas:httpmail:priority").Value = intImportance - 1
   Select Case intImportance
      Case 0
         .Fields("urn:schemas:mailheader:X-Priority").Value = 5
      Case 1
         .Fields("urn:schemas:mailheader:X-Priority").Value = 3
      Case 2
         .Fields("urn:schemas:mailheader:X-Priority").Value = 1
      Case Else
         .Fields("urn:schemas:mailheader:X-Priority").Value = 0
   End Select
   .Fields("urn:schemas:mailheader:X-MSMail-Priority").Value = intImportance
   If Not isNull(strSender) and strSender  strFrom then
      .Fields("urn:schemas:mailheader:return-path").Value = strSender
      .Fields("urn:schemas:mailheader:reply-to").Value = strFrom
   End If
   .Fields.Update()
   .To = strTo
   If Not isNull(strSender) and strSender  strFrom then
      .Sender = strSender
   End If
   If Not isNull(strBcc) Then
      .BCC = strBcc
   End If
   .From = strFrom
   .Subject = strSubject
   If CInt(intType) = 1 Then
      .TextBody = strBody
   Else
      .HTMLBody = strBody
   End If
   .Send()
End With
objMessage = Nothing
End Sub

This is generally useful for web generated emails (like send a friend forms etc.), simply specify the users address as strFrom and a generic local address (noreply@mydomain.com) as strSender.

More generally, here are some good CDO examples.