General discussion

Locked

Send e-mail with ASP.NET apps

By MaryWeilage Editor ·
This week's .NET newsletter demonstrates how ASP.NET makes it easy to utilize e-mail in an application with the System.Web.Mail namespace.

Have you found unique uses for e-mail or encountered problems with it in the ASP.NET environment?

If you haven't subscribed to our free .NET newsletter, sign up today! Click this link to subscribe automatically:
http://nl.com.com/MiniFormHandler?brand=techrepublic&list_id=e605

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

SMTP Authentication

by thanos911 In reply to Send e-mail with ASP.NET ...

I was wondering if anyone had any experience with the .NET Web class and sending out a message with SMTP Authentication. I have read several articles describing the CDO object and using the object schema to send out email, but I have not read anything about it working with IIS6/Windows 2003. Does anyone have working code for SMTP Authentication and the MailMessage object?

Collapse -

Send e-mail without using CDO or IIS SMTP service

by Andrew Houghton In reply to Send e-mail with ASP.NET ...

Many times in organizations you are unable to access CDO on production Web servers and the IIS SMTP service is forbidden to be installed on production Web servers or individual workstations for security reasons. However, you may still need to send mail in a production setting. With a little VB.NET (or C#.NET) you can just send SMTP messages to the server hosting your mail. For just these circumstances I wrote a simple VB.NET subroutine to send mail. You may need to adjust this subroutine, since it has a dependency on your SMTP server. That dependency is how you end the body of your message. My SMTP server, expects a dot on a line by itself. Consider the following source public domain.

Public Sub SendMail(ByVal smtpHost As String, ByVal fromEmail As String, ByVal toEmail As String, ByVal subject As String, ByVal message As String)

Dim client As TcpClient
Dim cns As NetworkStream

Dim reqBuffer(1024) As Byte
Dim rspBuffer(1024) As Byte

Dim reqMessage As String
Dim rspMessage As String

Dim nRead As Integer
Dim nWrite As Integer


Try
client = New TcpClient(smtpHost, 25)
cns = client.GetStream()

nRead = cns.Read(rspBuffer, 0, rspBuffer.Length)
rspMessage = Encoding.ASCII.GetString(rspBuffer, 0, nRead)

If Not rspMessage.StartsWith("220 ") Then
Throw New Exception("Expecting SMTP 220 response")
End If

reqMessage = "MAIL FROM: " & fromEmail & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

nRead = cns.Read(rspBuffer, 0, rspBuffer.Length)
rspMessage = Encoding.ASCII.GetString(rspBuffer, 0, nRead)

If Not rspMessage.StartsWith("250 ") Then
Throw New Exception("Expecting SMTP 250 response")
End If

reqMessage = "RCPT TO: " & toEmail & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

nRead = cns.Read(rspBuffer, 0, rspBuffer.Length)
rspMessage = Encoding.ASCII.GetString(rspBuffer, 0, nRead)

If Not rspMessage.StartsWith("250 ") Then
Throw New Exception("Expecting SMTP 250 response")
End If

reqMessage = "DATA" & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

nRead = cns.Read(rspBuffer, 0, rspBuffer.Length)
rspMessage = Encoding.ASCII.GetString(rspBuffer, 0, nRead)

If Not rspMessage.StartsWith("354 ") Then
Throw New Exception("Expecting SMTP 354 response")
End If

reqMessage = "From: " & Regex.Replace(fromEmail, "\s+", " ") & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

reqMessage = "To: " & Regex.Replace(toEmail, "\s+", " ") & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

reqMessage = "Subject: " & Regex.Replace(subject, "\s+", " ") & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

reqMessage = vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

reqMessage = message.Replace(vbCrLf, vbLf).Replace(vbLf & "." & vbLf, vbLf & ". " & vbLf)
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

reqMessage = vbLf & "." & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

nRead = cns.Read(rspBuffer, 0, rspBuffer.Length)
rspMessage = Encoding.ASCII.GetString(rspBuffer, 0, nRead)

If Not rspMessage.StartsWith("250 ") Then
Throw New Exception("Expecting SMTP 250 response")
End If

reqMessage = "QUIT" & vbLf
reqBuffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(reqMessage))

cns.Write(reqBuffer, 0, reqBuffer.Length)

nRead = cns.Read(rspBuffer, 0, rspBuffer.Length)
rspMessage = Encoding.ASCII.GetString(rspBuffer, 0, nRead)

If Not rspMessage.StartsWith("221 ") Then
Throw New Exception("Expecting SMTP 221 response")
End If

client.Close()

Catch e As Exception
Throw e
End Try

Exit Sub
End Sub

Collapse -

How to Send Encrypted e-mail with .NET

by gbpmail-builder In reply to Send e-mail with ASP.NET ...

How do I send encrypted e-mail using PKI certificates. There doesn't seem to be much info for ASP.NET. I've been trying to get Microsoft's http://support.microsoft.com/default.aspx?scid=kb;en-us;280391 example working but there seems to be code missing.

Regards,
Steven

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums