ram27
asked on
MailItem.get_Recipients
Here is my platform:
.net framewrok 1.1
Windows Xp Propfessional
MS Office 2007.
Visual Studio 2003.
Window based application.
I did below mentioned steps:
1. Right click on C# projeect, select "Add Referece"
2. Go to "COM" tab
3. Select "MS Office 12.0 Object Library" and "Microsoft Outlook 12.0 Object Library" and click on "OK".
4. i have added below code.
using Outlook=Microsoft.Office.I nterop.Out look;
pls see code below
public static void SendMail(string[] MailTo, string MailSubject, string MailBody, bool OpenEmailEditor,params string[] attachmentSource)
{
bool bResolve ;
string EmailDomain;
int position;
int attachType;
string displayName;
FileInfo fi;
EmailDomain = "@" + MyConfig.Get("EMail","Doma in");
Outlook.Recipient oRecipient;
Outlook.ApplicationClass myOutlook = new Outlook.ApplicationClass() ;
Outlook.MailItem newMail = (Outlook.MailItem)myOutloo k.CreateIt em(Outlook .OlItemTyp e.olMailIt em);
//Extract each recipient from Mailto and resolve them if they are valid id's
foreach (string s in MailTo)
{
if (s.Trim() != "")
{
if(s.IndexOf("@")== -1)
{
string str="";
str = s + EmailDomain;
oRecipient = newMail.Recipients.Add(str );
}
else
{
oRecipient = newMail.Recipients.Add(s); /* getting error */
}
bResolve = oRecipient.Resolve();
if (!bResolve || oRecipient.Name.EndsWith(E mailDomain ))
{
if (OpenEmailEditor == false)
{
throw new MySystemException(ConstFra meworkErro r.ERRCD_IN VALID_EMAI L_ID,new object[] {"",""});
}
else
{
newMail.Recipients.Remove( newMail.Re cipients.C ount);
newMail.Recipients.Add(s);
}
}
}
}
if(MailSubject != null)
{
newMail.Subject = MailSubject;
}
else
{
newMail.Subject = "";
}
if(MailBody != null)
{
newMail.Body = MailBody;
}
else
{
newMail.Body = "";
}
position = (int)MailBody.Length + 1;//Position of the attachment in the outlook editor.
attachType = (int)Outlook.OlAttachmentT ype.olByVa lue;
//Add all the attachments to new mail.
foreach(string path in attachmentSource)
{
//Add the attachment if file is present in the specified path.
if (File.Exists(path))
{
fi = new FileInfo(path);
displayName = fi.Name ;
newMail.Attachments.Add(pa th,attachT ype,positi on,display Name);
}
}
if (OpenEmailEditor == true)
{
newMail.Display(true);
}
else
{
newMail.Send();
}
oRecipient = null;
newMail = null;
myOutlook = null;
}
above mentioned function can be called in two ways.
First one see below code:
Here outlook editior will be opned when below code is executed and user will type to address and he will click "send" button on the outlook editor
string[] toAddress = new string[1];
string subject="test subject";
string messageBody ="test message body";
toAddress[0]= "";
SendMail(toAddress, subject,messageBody,true);
Second way:
in this case, outlook editor will not open and email will be sent to "toaddress" email automatically and will display message box saying that "email has been sent" etc.
string[] mailTo = new string[1];
string subject;
string mailBody;
string[] attachment = new string[1];
mailTo[0] = "mytestemail@xyz.com";
mailBody = "my test body";
subject="test subject";
attachment[0] = "c:\\myfile.xlsx";
SendMail(mailTo,subject,ma ilBody,fal se,attachm ent);<br/> MessageBox .Show("ema il sent sucessfully");
Both the ways it works when outlook instace is opened on the machine where i am testing.
When "outlook instace" is not opened, first way still works no problems. But when i call second way am getting an exception "UnHandled Exception,999,Operation aborted,Microsoft.Office.I nterop.Out look._Mail Item.get_R ecipients( )".
If outlook instace is opened i did not encounter any errors but when out look is not opened at that when am testing above second way( i..e, SendMail(mailTo,subject,ma ilBody,fal se,attachm ent);), i am getting error. please help.
Please let me know what is the problem.
Thanks,
.net framewrok 1.1
Windows Xp Propfessional
MS Office 2007.
Visual Studio 2003.
Window based application.
I did below mentioned steps:
1. Right click on C# projeect, select "Add Referece"
2. Go to "COM" tab
3. Select "MS Office 12.0 Object Library" and "Microsoft Outlook 12.0 Object Library" and click on "OK".
4. i have added below code.
using Outlook=Microsoft.Office.I
pls see code below
public static void SendMail(string[] MailTo, string MailSubject, string MailBody, bool OpenEmailEditor,params string[] attachmentSource)
{
bool bResolve ;
string EmailDomain;
int position;
int attachType;
string displayName;
FileInfo fi;
EmailDomain = "@" + MyConfig.Get("EMail","Doma
Outlook.Recipient oRecipient;
Outlook.ApplicationClass myOutlook = new Outlook.ApplicationClass()
Outlook.MailItem newMail = (Outlook.MailItem)myOutloo
//Extract each recipient from Mailto and resolve them if they are valid id's
foreach (string s in MailTo)
{
if (s.Trim() != "")
{
if(s.IndexOf("@")== -1)
{
string str="";
str = s + EmailDomain;
oRecipient = newMail.Recipients.Add(str
}
else
{
oRecipient = newMail.Recipients.Add(s);
}
bResolve = oRecipient.Resolve();
if (!bResolve || oRecipient.Name.EndsWith(E
{
if (OpenEmailEditor == false)
{
throw new MySystemException(ConstFra
}
else
{
newMail.Recipients.Remove(
newMail.Recipients.Add(s);
}
}
}
}
if(MailSubject != null)
{
newMail.Subject = MailSubject;
}
else
{
newMail.Subject = "";
}
if(MailBody != null)
{
newMail.Body = MailBody;
}
else
{
newMail.Body = "";
}
position = (int)MailBody.Length + 1;//Position of the attachment in the outlook editor.
attachType = (int)Outlook.OlAttachmentT
//Add all the attachments to new mail.
foreach(string path in attachmentSource)
{
//Add the attachment if file is present in the specified path.
if (File.Exists(path))
{
fi = new FileInfo(path);
displayName = fi.Name ;
newMail.Attachments.Add(pa
}
}
if (OpenEmailEditor == true)
{
newMail.Display(true);
}
else
{
newMail.Send();
}
oRecipient = null;
newMail = null;
myOutlook = null;
}
above mentioned function can be called in two ways.
First one see below code:
Here outlook editior will be opned when below code is executed and user will type to address and he will click "send" button on the outlook editor
string[] toAddress = new string[1];
string subject="test subject";
string messageBody ="test message body";
toAddress[0]= "";
SendMail(toAddress, subject,messageBody,true);
Second way:
in this case, outlook editor will not open and email will be sent to "toaddress" email automatically and will display message box saying that "email has been sent" etc.
string[] mailTo = new string[1];
string subject;
string mailBody;
string[] attachment = new string[1];
mailTo[0] = "mytestemail@xyz.com";
mailBody = "my test body";
subject="test subject";
attachment[0] = "c:\\myfile.xlsx";
SendMail(mailTo,subject,ma
Both the ways it works when outlook instace is opened on the machine where i am testing.
When "outlook instace" is not opened, first way still works no problems. But when i call second way am getting an exception "UnHandled Exception,999,Operation aborted,Microsoft.Office.I
If outlook instace is opened i did not encounter any errors but when out look is not opened at that when am testing above second way( i..e, SendMail(mailTo,subject,ma
Please let me know what is the problem.
Thanks,
Hi
I think, "From" part is missing
http://classicasp.aspfaq.com/email/how-do-i-send-e-mail-in-html-format.html
I think, "From" part is missing
http://classicasp.aspfaq.com/email/how-do-i-send-e-mail-in-html-format.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.codeproject.com/KB/cs/Sending_Mails_From_C_.aspx
I think your problem is that you are creating an Outlook application every time you send mail (I will make it a field for the lifetime of your form depending on usage) and this object is not being proerly released. If you do operations multiple times it will kill Outlook because Outlook works in a STA thread COM model.
Thus, as a first improvement, you need to release the com objects, before setting to null and force Garbage collection.
Marshal.ReleaseComObject(o
Marshal.ReleaseComObject(n
Marshal.ReleaseComObject(m
oRecipient = null;
newMail = null;
myOutlook = null;
GC.Collect();
GC.WaitForPendingFinalizer