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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
using System;
using System.Configuration;
using System.Reflection;
using System.Threading;
namespace GenDossierPdf
{
/// <summary>
/// La classe ThreadMail facilite l'envoi de message via Exchange.
/// L'envoi de message doit être réalisé dans une thread
/// utilisant le mode de cloisonnement STA.
/// </summary>
public class ThreadMail
{
public string[] destinataires;
public string sujet;
public string texte;
public string nomFichier;
public MAPI.Session sessionExchange;
public Exception exceptionLevee = null;
public ThreadMail(MAPI.Session sessionExch,string dest,string suj, string corps, string fichierJoint)
{
destinataires = dest.Split(';');
sujet = suj;
texte = corps;
nomFichier = fichierJoint;
sessionExchange = sessionExch;
}
public void sendMail()
{
string Server;
string Mailbox;
object ProfileInfo;
// Use vEmpty for Empty parameter.
Object vEmpty = Missing.Value;
Server = ConfigurationSettings.AppSettings["serveurExchange"];
Mailbox = ConfigurationSettings.AppSettings["utilisateurExchange"];
ProfileInfo = Server + (char)10 + Mailbox;
try
{
if (sessionExchange==null)
{
Thread.CurrentThread.ApartmentState = ApartmentState.STA;
// Create MAPI session.
sessionExchange = (MAPI.Session)Activator.CreateInstance(Type.GetTypeFromProgID("MAPI.Session"));
// Log on to MAPI.
sessionExchange.Logon(System.Environment.UserName, vEmpty, false, true, 0, true, ProfileInfo);
}
// Get Outbox.
MAPI.Folder oFolder = (MAPI.Folder)sessionExchange.Outbox;
// Get Messages collection.
MAPI.Messages oMsgs = (MAPI.Messages)oFolder.Messages;
// Create a new message and populate some common properties.
MAPI.Message oMsg = (MAPI.Message)oMsgs.Add(vEmpty, vEmpty, vEmpty, vEmpty);
oMsg.Subject = sujet;
oMsg.Text = texte;
// Add Attachment
if (nomFichier != "")
{
MAPI.Attachments oAtts = (MAPI.Attachments)oMsg.Attachments;
MAPI.Attachment oAtt = (MAPI.Attachment)oAtts.Add(vEmpty,vEmpty,vEmpty,vEmpty);
oAtt.Source = nomFichier;
oAtt.Name = System.IO.Path.GetFileName(nomFichier);
oAtt.Type = MAPI.CdoAttachmentType.CdoFileData;
oAtt.ReadFromFile(nomFichier);
}
if (ConfigurationSettings.AppSettings["debug"] == "1")
{
destinataires = ConfigurationSettings.AppSettings["adminEmail"].Split(';');
}
MAPI.Recipients oRecips = (MAPI.Recipients)oMsg.Recipients;
MAPI.Recipient oRecip;
for (int i=0;i<destinataires.Length;i++)
{
// Add a new recipient.
oRecip = (MAPI.Recipient)oRecips.Add(vEmpty, vEmpty, vEmpty, vEmpty);
oRecip.Name = destinataires[i];
oRecip.Resolve(false);
}
oMsg.Importance = 2; // Importance Haute
// Envoi en tant qu'un autre utilisateur Exchange (délégué)
MAPI.AddressLists oAddrLists;
MAPI.AddressList oAddrList;
MAPI.AddressEntries oAddrEntries;
MAPI.AddressEntryFilter oAddrEntryFilter;
if (ConfigurationSettings.AppSettings["expedierEnTantQue"] != "")
{
oAddrLists = (MAPI.AddressLists)sessionExchange.AddressLists;
oAddrList = (MAPI.AddressList)oAddrLists.get_Item(1);
oAddrEntries = (MAPI.AddressEntries)oAddrList.AddressEntries;
// oAddrEntries.Filter = null;
oAddrEntryFilter = (MAPI.AddressEntryFilter)oAddrEntries.Filter;
oAddrEntryFilter.Name = ConfigurationSettings.AppSettings["expedierEnTantQue"];
oMsg.Sender = (MAPI.AddressEntry)oAddrEntries.GetFirst();
}
oMsg.Update(true,true);
oMsg.Send(false, false, 0);
// Clean up.
oRecip = null;
oRecips = null;
oMsgs = null;
oMsg = null;
oFolder = null;
oAddrLists = null;
oAddrList = null;
oAddrEntries =null;
oAddrEntryFilter = null;
}
catch (Exception e)
{
exceptionLevee = e;
}
}
} // Fin classe ThreadMail
} |
Partager