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=Microso ft.Office.Inter op.Outlook;
pls see code below
public static void SendMail(string[] MailTo, string MailSubject, string MailBody, bool OpenEmailEditor ,params string[] attachmentSourc e)
{
bool bResolve ;
string EmailDomain;
int position;
int attachType;
string displayName;
FileInfo fi;
EmailDomain = "@" + MyConfig.Get("E Mail","Domain") ;
Outlook.Recipie nt oRecipient;
Outlook.Applica tionClass myOutlook = new Outlook.Applica tionClass();
Outlook.MailIte m newMail = (Outlook.MailIt em)myOutlook.Cr eateItem(Outloo k.OlItemType.ol MailItem);
//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.Recipie nts.Add(str);
}
else
{
oRecipient = newMail.Recipie nts.Add(s); /* getting error */
}
bResolve = oRecipient.Reso lve();
if (!bResolve || oRecipient.Name .EndsWith(Email Domain))
{
if (OpenEmailEdito r == false)
{
throw new MySystemExcepti on(ConstFramewo rkError.ERRCD_I NVALID_EMAIL_ID ,new object[] {"",""});
}
else
{
newMail.Recipie nts.Remove(newM ail.Recipients. Count);
newMail.Recipie nts.Add(s);
}
}
}
}
if(MailSubject != null)
{
newMail.Subject = MailSubject;
}
else
{
newMail.Subject = "";
}
if(MailBody != null)
{
newMail.Body = MailBody;
}
else
{
newMail.Body = "";
}
position = (int)MailBody.L ength + 1;//Position of the attachment in the outlook editor.
attachType = (int)Outlook.Ol AttachmentType. olByValue;
//Add all the attachments to new mail.
foreach(string path in attachmentSourc e)
{
//Add the attachment if file is present in the specified path.
if (File.Exists(pa th))
{
fi = new FileInfo(path);
displayName = fi.Name ;
newMail.Attachm ents.Add(path,a ttachType,posit ion,displayName );
}
}
if (OpenEmailEdito r == 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(toAddr ess, subject,message Body,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@xy z.com";
mailBody = "my test body";
subject="test subject";
attachment[0] = "c:\\myfile.xls x";
SendMail(mailTo ,subject,mailBo dy,false,attach ment);<br/>MessageBox.Sho w("email 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,O peration aborted,Microso ft.Office.Inter op.Outlook._Mai lItem.get_Recip ients()".
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,mailBo dy,false,attach ment);), 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=Microso ft.Office.Inter op.Outlook;
pls see code below
public static void SendMail(string[] MailTo, string MailSubject, string MailBody, bool OpenEmailEditor ,params string[] attachmentSourc e)
{
bool bResolve ;
string EmailDomain;
int position;
int attachType;
string displayName;
FileInfo fi;
EmailDomain = "@" + MyConfig.Get("E Mail","Domain") ;
Outlook.Recipie nt oRecipient;
Outlook.Applica tionClass myOutlook = new Outlook.Applica tionClass();
Outlook.MailIte m newMail = (Outlook.MailIt em)myOutlook.Cr eateItem(Outloo k.OlItemType.ol MailItem);
//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.Recipie nts.Add(str);
}
else
{
oRecipient = newMail.Recipie nts.Add(s); /* getting error */
}
bResolve = oRecipient.Reso lve();
if (!bResolve || oRecipient.Name .EndsWith(Email Domain))
{
if (OpenEmailEdito r == false)
{
throw new MySystemExcepti on(ConstFramewo rkError.ERRCD_I NVALID_EMAIL_ID ,new object[] {"",""});
}
else
{
newMail.Recipie nts.Remove(newM ail.Recipients. Count);
newMail.Recipie nts.Add(s);
}
}
}
}
if(MailSubject != null)
{
newMail.Subject = MailSubject;
}
else
{
newMail.Subject = "";
}
if(MailBody != null)
{
newMail.Body = MailBody;
}
else
{
newMail.Body = "";
}
position = (int)MailBody.L ength + 1;//Position of the attachment in the outlook editor.
attachType = (int)Outlook.Ol AttachmentType. olByValue;
//Add all the attachments to new mail.
foreach(string path in attachmentSourc e)
{
//Add the attachment if file is present in the specified path.
if (File.Exists(pa th))
{
fi = new FileInfo(path);
displayName = fi.Name ;
newMail.Attachm ents.Add(path,a ttachType,posit ion,displayName );
}
}
if (OpenEmailEdito r == 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(toAddr ess, subject,message Body,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@xy z.com";
mailBody = "my test body";
subject="test subject";
attachment[0] = "c:\\myfile.xls x";
SendMail(mailTo ,subject,mailBo dy,false,attach ment);<br/>MessageBox.Sho w("email 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,O peration aborted,Microso ft.Office.Inter op.Outlook._Mai lItem.get_Recip ients()".
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,mailBo dy,false,attach ment);), i am getting error. please help.
Please let me know what is the problem.
Thanks,