473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

send email with attachment using Microsoft Outlook Object Library 2007

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.Interop.Outlook;


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","Domain");

Outlook.Recipient oRecipient;

Outlook.ApplicationClass myOutlook = new Outlook.ApplicationClass();

Outlook.MailItem newMail = (Outlook.MailItem)myOutlook.CreateItem(Outlook.OlI temType.olMailItem);

//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(EmailDomain))

{

if (OpenEmailEditor == false)

{

throw new MySystemException(ConstFrameworkError.ERRCD_INVALI D_EMAIL_ID,new object[] {"",""});

}

else

{

newMail.Recipients.Remove(newMail.Recipients.Count );

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.OlAttachmentType.olByValue;

//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(path,attachType,position,d isplayName);

}

}

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,mailBody,false,attachment) ;<br/>MessageBox.Show("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,Operation aborted,Microsoft.Office.Interop.Outlook._MailItem .get_Recipients()".

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,mailBody,false,attachment) ;), i am getting error. please help.
Please let me know what is the problem.
Thanks,
Feb 1 '10 #1
0 3425

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fred | last post by:
Is there a good code sample, in C#, showing how to send eamil with attachment, from a Windows Form like 'Send To' in Word, that will pickup Contacts in Outlook/Exchange 2000. I have searched...
8
by: John Brock | last post by:
I am currently using a VB.NET program to send out e-mails with plain text bodies (plus one attachment). The emails are being received as Rich Text messages (probably just my personal Outlook...
5
by: J-P-W | last post by:
I have some code, from this group (many thanks) that sends an attachment to an email. The following: Dim objNewMail As Outlook.MailItem Dim golApp As...
3
by: sentiboy | last post by:
I am using the following code for sending email through mapi in VS 2005 environment: Public objSession As New MSMAPI.MAPISession Public objMessage As New MSMAPI.MAPIMessages ...
16
by: Kosmos | last post by:
Good afternoon everyone, just wondering if anyone knew if it's possible to send meetings or appointments through email when you run VBA or SQL code in Access 2003? The following is the code I've been...
1
by: bamrak | last post by:
I seem to have gotten over my head, but this seems fairly straightforward.. I have a table of items assigned to a rep, and I have a rep list table created from the original table ( to show only...
5
by: pat | last post by:
Hi, i'am using an ac2K progrom which sends automaticle emails with an attachment. Since last week , a error occurs with number 2293. Our mail program is Outlook 2003. It's probably a matter...
2
by: ManningFan | last post by:
I built a nifty little SPAM killer with Access linking into Outlook while I was at work the other day (boring day, loads of time to kill...) but now I'd like to use it at home where we use Outlook...
1
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the results to just the email addresses that the query...
0
by: BuntyJ | last post by:
I have made an mail application to send mail. I am using richtextbox ver.5 for the body of mail. Most of the things is going fine, but i am facing one problem as follow: When user open an outlook...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.