Inviare il contenuto html di una pagina web via email
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<script runat="server">
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim message As String = ""
Try
message = leggiHTML(txtURL.Text)
Catch
lblResult.Text = "Inserisci un url"
End Try
If (Page.IsValid) Then
Try
Dim Mailer As MailMessage = New MailMessage
Mailer.From = txtFrom.Text
Mailer.To = txtTo.Text
Mailer.Subject = txtSubject.Text
Mailer.Body = message
Mailer.BodyFormat = System.Web.Mail.MailFormat.Html
SmtpMail.Send(Mailer)
lblResult.Text = "Pagina inviata!"
Catch
lblResult.Text = "Errore nell'invio"
End Try
End If
End Sub
Private Function leggiHTML(ByVal url As String) As String
Dim result As String
Dim objResponse As WebResponse
Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
objResponse = objRequest.GetResponse()
Dim sr As StreamReader = New StreamReader(objResponse.GetResponseStream())
result = sr.ReadToEnd()
sr.Close()
Return result
End Function
</script>
<body>
<form id="Form1" method="post" runat="server">
<table cellSpacing="0" cellPadding="4">
<tr>
<td>URL:
</td>
<td><asp:textbox id="txtURL" runat="server" Width="200px">http://</asp:textbox></td>
</tr>
<tr>
<td>Invia a:
</td>
<td><asp:textbox id="txtTo" runat="server" Width="200px"></asp:textbox></td>
</tr>
<tr>
<td>La tua mail:
</td>
<td><asp:textbox id="txtFrom" runat="server" Width="200px"></asp:textbox></td>
</tr>
<tr>
<td>Oggetto:
</td>
<td><asp:textbox id="txtSubject" runat="server" Width="200px"></asp:textbox></td>
</tr>
</table>
<BR>
<asp:button id="btnSend" onclick="btnSend_Click" runat="server" Text="Invia"></asp:button>
<asp:label id="lblResult" Runat="server"></asp:label></form>
</body>
</HTML>