Asp.net - envoyer un mail avec jmail.net

Contenu du snippet

jmail.net free dispo à l'adresse :

http://www.dimac.net/default2.asp ...

téléchargez le et installez le sur votre serveur

tu crées un repertoire bin sous la racine de ton site ou tu mets les 2 dll

Dimac.JMail.dll
Dimac.JMail.Smtp.dll ( c'est ce qui te permets d'envoyer avec un compte externe )

et la page qui va bien avec le test sur la validité de l'email

voila

Happy coding

Eric

Conclusion :


<%@ Page Language="VB" validaterequest="false" Debug="true" %>
<%@ import Namespace="Dimac.JMail" %>
<script runat="server">

Sub SendMessage( ByVal sender As Object, Byval e As EventArgs )

If (Page.IsValid) Then
lblOutput.Text = "Page valide !"
Else
lblOutput.Text = "Page non valide ! :-("
End If
Dim message as new Message()
message.From.Email = tFrom.Text
message.To.Add( tTo.Text, "" )
message.Subject = tSubject.Text
message.Charset = System.Text.Encoding.GetEncoding( "iso-8859-1" )
IF cbIsHtml.Checked
message.BodyHtml = tBody.Text
Else
message.BodyText = tBody.Text
End If

if Not ( iAttachment.PostedFile Is Nothing ) And iAttachment.PostedFile.FileName <> ""
Dim fileName = System.IO.Path.GetFileName( iAttachment.PostedFile.FileName )
message.Attachments.Add( iAttachment.PostedFile.InputStream, fileName )
End If

Try
Dim selected = [Enum].Parse( GetType( SmtpAuthentication ), "Login" )

Smtp.Send( message, "votre serveur smtp", 25, GetDomain( message.From.Email ), selected, "votre login", "votre mot de passe" )

lResult.Text = "Message has been sent!"
Catch ex As Exception
lResult.Text = "An exception occured: " & ex.Message
End Try

End Sub

' Get the domain part of an email address.
Private Function GetDomain( ByVal email As String ) As String
Dim index = email.IndexOf( "@" )
Return email.Substring( index + 1 )
End Function

' Add encoding to hash table
Private Sub AddEncoding( ByVal encoding As System.Text.Encoding, ByRef table As System.Collections.Hashtable )
if Not table.ContainsKey( encoding.BodyName )
table.Add( encoding.BodyName, encoding.EncodingName )
End If
End Sub
Public Function IsEmailValid(ByVal chaine As String) As Boolean
If (chaine <> "") Then
Dim RegexpEmail As New Regex("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
Dim bEmailValid As Boolean = RegexpEmail.IsMatch(chaine)
If (bEmailValid) Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function

</script>
<html>
<head>
<title>SMTP Example</title>
</head>
<body>
<form id="tForm" method="post" enctype="multipart/form-data" runat="server">
<table border="0">
<tbody>
<tr>
<td colspan="2">
<asp:Label ID="lblOutput" Text="Nous contacter" Font-Name="Verdana" Font-Size="10pt" runat="server" />
</td>
</tr>
<tr>
<td>
From:</td>
<td>
<asp:textbox id="tFrom" runat="server" columns="50"></asp:textbox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="tFrom"
Display="Dynamic"
Font-Name="Verdana" Font-Size="10pt"
>
*
</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
ControlToValidate="tFrom"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Static"
Font-Name="verdana" Font-Size="10pt">
Veuillez entrer une adresse électronique valide
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
To:</td>
<td>
<asp:textbox id="tTo" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td>
Subject:</td>
<td>
<asp:textbox id="tSubject" runat="server" columns="50"></asp:textbox>
</td>
</tr>
<tr>
<td valign="top">
Body:</td>
<td>
<asp:textbox id="tBody" runat="server" columns="50" textmode="multiline" rows="5"></asp:textbox>
</td>
</tr>
<tr>
<td>
Encoding:</td>
<td>
<asp:checkbox id="cbIsHtml" runat="server" text="HTML"></asp:checkbox>
</td>
<td>
</td>
</tr>
<tr>
<td>
Attachment:</td>
<td>
<input id="iAttachment" type="file" runat="server" /></td>
</tr>
<tr>
<td colspan="2">
<asp:Label id="lResult" runat="server" text=" " font-bold="true"></asp:Label></td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:button id="bSend" onclick="SendMessage" runat="server" text="Send"></asp:button>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.