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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net.Mail;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.Services.Description;
public partial class Envoi : System.Web.UI.Page
{
DataSet ds = new DataSet(); // objet qui reside en memoire et qui correspond à une copie locale de la base
SqlCommand cmd = new SqlCommand(); // pour l execusion de la requete
SqlConnection con; // connection
SmtpClient smtp = null; // Simple Mail Transfer Protocol
SqlDataReader oReader = null; // un moyen de lire un flux de lignes a partir d'une base de données sql
SqlConnection oConnexion = null; //pour ouvrir une session de connexion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // obtenir de l'info sur la lecture de la page
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
oConnexion = new SqlConnection("Data Source=localhost;Initial Catalog=aymen_mailing;Integrated Security=True"); //chemin de la base
oConnexion.Open();
SqlCommand oCommande = new SqlCommand();
oCommande.Connection = oConnexion;
oCommande.CommandText = "SELECT email FROM entreprise";
oReader = oCommande.ExecuteReader();
lstEmail.Items.Clear();
if (oReader.Read())
sendMail();
else
{
oConnexion.Close();
oConnexion.Dispose();
}
}
void sendMail()
{
MailMessage email = new MailMessage();
string chemin;
chemin = "C:\\Documents and Settings\\Administrateur\\Bureau\\questionnaire\\questionnaire.html";
email.Attachments.Add(new Attachment(chemin));
email.From = new MailAddress(Expediteur.Text);
email.To.Add(oReader["email"].ToString());
email.Subject = Objet.Text;
email.Body = Message.Text;
email.IsBodyHtml = false;
smtp = new SmtpClient(ConfigurationSettings.AppSettings["SmtpServeur"], 50);
smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); // la confirmation ke l'envoi est fé completemen
try
{
smtp.SendAsync(email, lstEmail.Text); // envoi un par un
erreur.Text = ("email envoyé avec succes");
}
catch (Exception ex)
{
erreur.Text = ex.Message;
if (oReader.Read())
sendMail(); // methode pour l'envoi
else
{
oConnexion.Close();
oConnexion.Dispose();
}
}
}
public void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
if (oReader.Read())
{
sendMail();
}
else
{
oConnexion.Close();
oConnexion.Dispose();
}
}
protected void categorie_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void lstEmail_SelectedIndexChanged(object sender, EventArgs e)
{
}
} |
Partager