Bonjour,
Je suis en train de tester l'envoi de mail via mon site (hébergé sur Free). Je ne sais même si ça peut marcher d'ailleurs. Pour tester j'essaie de mettre en oeuvre un exemple trouvé sur le net.
J'ai donc créé un nouveau projet appelé SendEmail. Dans le projet SendEmail.Web, j'ai créé un Service Web appelé MailServ.asmx dont le contenu est :
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net.Mail;
 
namespace SendEmail.Web
{
    [WebService(Namespace = "http://monsite.free.fr/")]
 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class MailServ : System.Web.Services.WebService
    {
 
        [WebMethod]
        public bool Send(string fromEmail, string toEmail, string subject, string body)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress(fromEmail);
                message.To.Add(new MailAddress(toEmail));
                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.EnableSsl = true;
                smtp.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

J'ai ajouté ce service en tant que Service Web au projet SendEmail. Je l'ai appelé Proxy.
Le code de ma MainPage est le suivant :
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
 
namespace SendEmail
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            BasicHttpBinding bind = new BasicHttpBinding();
            EndpointAddress endpoint = new EndpointAddress("http://localhost:51639/SendEmailWeb/MailServ.asmx");
            Proxy.MailServSoapClient mailService = new SendEmail.Proxy.MailServSoapClient(bind, endpoint);
            mailService.SendAsync("monsite@free.fr", "destinataire@hotmail.fr", "Test mail", "Alors ca marche ?");
            mailService.SendCompleted += new EventHandler<Proxy.SendCompletedEventArgs>(mailService_SendCompleted);
 
        }
        void mailService_SendCompleted(object sender, Proxy.SendCompletedEventArgs e)
        {
            if (e.Result)
            {
                resultTextBlock.Foreground = new SolidColorBrush(Colors.Blue);
                resultTextBlock.Text = "Your email has been sent successfully!";
            }
            else
            {
                resultTextBlock.Foreground = new SolidColorBrush(Colors.Red);
                resultTextBlock.Text = "Sending failed.";
            }
        }
 
    }
}
N.B. : monsite@free.fr est l'adresse free que j'ai créée
Enfin j'ai configuré le fichier Web.config de la manière suivante :
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.net>
    <mailSettings>
      <smtp>
        <network host="smtp.free.fr" port="25" userName="monsite@free.fr" password="monpassword"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
A l'éxécution, j'ai le message suivant :
Une erreur s'est produite en tentant d'effectuer une demande à l'URI 'http://localhost:51639/SendEmailWeb/MailServ.asmx'. Ce problème peut être dû à une tentative d'accès à un service entre domaines sans qu'une stratégie entre domaines appropriée soit en place, ou une stratégie inadaptée aux services SOAP. Il est possible que vous soyez contraint de contacter le propriétaire du service pour publier un fichier de stratégie entre domaines et veiller à ce qu'il autorise l'envoi d'en-têtes HTTP SOAP. Cette erreur peut également être liée à l'utilisation de types internes dans le proxy de service Web sans utiliser l'attribut InternalsVisibleToAttribute. Consultez l'exception interne pour plus de détails.
En testant sur mon site Free directement, j'ai pas le message mais je ne reçois pas le mail.

En espérant que vous puissiez m'aider.
Merci beaucoup
Cédric