How to add multiple recipients to mailitem.cc field c# - c#

Oki, so im working on outlook .msg templates.
Opening them programmatically, inserting values base on what's in my db.
ex. when i want to add multiple reciepients at "To" field, instead of doing as following,
mailitem.To = a + ";" + b + ";" + c;
i do whats below, which is simpler, especially when i'm doing it in a loop.
mailitem.Recipients.add("a");
mailitem.Recipients.add("b");
mailitem.Recipients.add("c");
My problem is, i also want to add multiple recipients at "CC" field and the function above only works for "To" field. How can i add multiple recipients to "CC" field without having to do string manipulation.
normally i would add recipients to cc like so,
mailitem.CC = a + ";" + b + ";" + c;
im using interop.outlook and creating an mailitem from template.
Thanks in advance.

Suppose If you have two List of recipients, then you can do like this.
Edit: Included full code.
var oApp = new Microsoft.Office.Interop.Outlook.Application();
var oMsg = (MailItem) oApp.CreateItem(OlItemType.olMailItem);
Recipients oRecips = oMsg.Recipients;
List<string> sTORecipsList = new List<string>();
List<string> sCCRecipsList = new List<string>();
sTORecipsList.Add("ToRecipient1");
sCCRecipsList.Add("CCRecipient1");
sCCRecipsList.Add("CCRecipient2");
sCCRecipsList.Add("CCRecipient3");
Recipients oRecips = oMsg.Recipients;
foreach (string t in sTORecipsList)
{
Recipient oTORecip = oRecips.Add(t);
oTORecip.Type = (int) OlMailRecipientType.olTo;
oTORecip.Resolve();
}
foreach (string t in sCCRecipsList)
{
Recipient oCCRecip = oRecips.Add(t);
oCCRecip.Type = (int) OlMailRecipientType.olCC;
oCCRecip.Resolve();
}
oMsg.HTMLBody = "Test Body";
oMsg.Subject = "Test Subject";
oMsg.Send();

Use the Recipients property as documented here (look for the second example). you can add a lot of people to the collection and then change the destination type from to to CC.

Related

How can I add a party list field to an entity in CRM WebAPI?

When I'm going to create a campaign response entity, I need to add a party list attribute, the key for this attribute is "campaignresponse_activity_parties" in the API.
This old code explain what I want to do:
Entity party1 = new Entity("activityparty");
party1["addressused"] = email;
party1["partyid"] = new EntityReference("lead", lead.Id);
EntityCollection partyList = new EntityCollection();
partyList.Entities.Add(party1);
campaignResponse["customer"] = partyList;
I tried some ways around the internet like this, but unsuccessfully, I'm not given an error when creating the campaign response, but the party list field is not added to the campaign response entity record.
My current code is something like this:
JObject party1 = new JObject();
party1["addressused"] = email;
party1["partyid_lead#odata.bind"] = "/leads(" + lead.Id.ToString() + ")";
JArray partyList = new JArray();
partyList.Add(party1);
campaignResponses["campaignresponse_activity_parties"] = partyList;
If there's anything that I forgot to do, please let me know.
You have to add participationtypemask as well. I’m not quite sure on number (11 or 4) and I’m answering without testing this code from my mobile right now.
JObject party1 = new JObject();
party1["addressused"] = email;
party1["partyid_lead#odata.bind"] = "/leads(" + lead.Id.ToString() + ")";
party1["participationtypemask"] = 11;
JArray partyList = new JArray();
partyList.Add(party1);
campaignResponses["campaignresponse_activity_parties"] = partyList;
Reference

Retrieving email address from a globalAddressList in outlook

Based on the description for the AddressEntry.Address Property, I expect the following to "return a String (string in C#) representing the e-mail address of the AddressEntry."
Outlook.AddressList gal = Application.Session.GetGlobalAddressList();
Outlook.AddressEntries ae = gal.AddressEntries;
List<string> email = new List<string>();
foreach (Outlook.AddressEntry e in ae)
{
email.Add(e.Address);
}
Rather, the email list fills up with strings that look like...
"/o=companyName/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=firstname.lastname"
I would prefer it return something like....
firstname.lastname#mycompany.com
How am I using this incorrectly?
If AddressEntry.Type == "EX", use AddressEntry.GetExchangeUser().PrimarySmtpAddress. Be prepared to handle nulls and exceptions.

Iterating whole list and updating string using ToLower - Not working

Technologies using.
C#
.NET 4.0
Visual Studio 2010
Problem.
I have a List<User> which contains an Email property. I want to lowercase all the email addresses within the list, but my implementation is not working. I'm using the following statement:
emails.ToList().ForEach(e => e.ToLower());
This didnt work at all for email addresses like Catherine.Burke#email.co.uk. I built the following to test this:
string email = "Catherine.Burke#email.co.uk";
email = email.ToLower();
Console.WriteLine("Email: " + email);
string email2 = "Catherine.Burke#email.co.uk";
string email3 = "Gareth.bradley#email.co.uk";
List<string> emails = new List<string>();
emails.Add(email2);
emails.Add(email3);
emails.ToList().ForEach(e => e.ToLower());
emails.ToList().ForEach(delegate(string e)
{
Console.WriteLine("ForEach deletegate : " + e);
});
List<EmailAddress> emailAddresses = new List<EmailAddress>();
emailAddresses.Add(new EmailAddress { FullAddress = "Catherine.Burke#email.co.uk" });
emailAddresses.Add(new EmailAddress { FullAddress = "Gareth.bradley#email.co.uk" });
emailAddresses.ToList().ForEach(e => e.FullAddress.ToLower());
emailAddresses.ToList().ForEach(delegate(EmailAddress e)
{
Console.WriteLine("EmailAddress delegate: " + e.FullAddress);
});
foreach (EmailAddress em in emailAddresses)
{
Console.WriteLine("Foreach Print: " + em.FullAddress);
}
Now I thought it might be the Culture and as these are names, it kept them uppercase, but when I used ToLower() on a singular string it worked. The above ran with the following output, as you can see the 1st line shows an email address with lowercase characters, whereas the implementation of the various List's I tried using ForEach() have not worked. I'm presuming my implementation of ForEach() is incorrect?
Making my comment an answer as requested:
Use a simple for-loop. List.ForEach is a method where you get the string as argument, you can't replace the whole reference there and since strings are immutable you can't change them either. You have to reassign the string returned from String.ToLower to your variable:
for(int i = 0; i < emails.Count; i++)
emails[i] = emails[i].ToLower();
Side-note: if you are making all emails lowercase to get a case-insensitive comparison it's better to use the String.Equals overload with the right StringComparison
string email1 = "Catherine.Burke#email.co.uk";
string email2 = "catherine.burke#email.co.uk";
if (String.Equals(email1, email2, StringComparison.InvariantCultureIgnoreCase))
{
// ...
}
emails.ToList().ForEach(e => e.ToLower()); does just call ToLower() but does not assign the result.
What you want is:
var lowerEmails = emails.Select(e => e.ToLower()).ToList();
Try this:
emailAddresses.ToList().ForEach(e => e.FullAddress = e.FullAdress.ToLower());
As weertzui altready mentions the ForEach-method simply calls the delegate. However the result of this action is not used in your code in any way.
However I´d strongly recommend to use a simply foreach:
foreach(var mail in emailadresses) mail.FullAdress = mail.FullAdress.ToLower();
which seems better readable to me.

How to get the recipients of an email using EWS

I am struggling to get the recipients of an email.
I understand that the Recipients is an array, so I need to put them into an array, but my code will not compile:
do
{
// set the prioperties we need for the entire result set
view.PropertySet = new PropertySet(
BasePropertySet.IdOnly,
ItemSchema.Subject,
ItemSchema.DateTimeReceived,
ItemSchema.DisplayTo, EmailMessageSchema.ToRecipients,
EmailMessageSchema.From, EmailMessageSchema.IsRead,
EmailMessageSchema.HasAttachments, ItemSchema.MimeContent,
EmailMessageSchema.Body, EmailMessageSchema.Sender,
ItemSchema.Body) { RequestedBodyType = BodyType.Text };
// load the properties for the entire batch
service.LoadPropertiesForItems(results, view.PropertySet);
e2cSessionLog("\tcommon.GetUnReadMailAll", "retrieved " + results.Count() + " emails from Mailbox (" + common.strInboxURL + ")");
foreach (EmailMessage email in results)
// looping through all the emails
{
emailSenderName = email.From.Address;
sEmailSubject = email.Subject;
emailDateTimeReceived = email.DateTimeReceived.ToShortDateString();
emailHasAttachments = email.HasAttachments;
ItemId itemId = email.Id;
emailDisplayTo = email.DisplayTo;
sEmailBody = email.Body; //.Text;
Recipients = email.ToRecipients;
....
the last line there will not compile, as apparently I cannot implicitly convert the collection ToRecipients to a string...
so I tried to loop through all the ToRecipients:
string[] Recipients;
for (int iIdx=0; iIdx<-email.ToRecipients.Count; iIdx++)
{
Recipients[iIdx] = email.ToRecipients[iIdx].ToString();
}
but I have obviously not declare this properly, as it won't compile with the message that Recipients is unassigned.
What is the correct way to assign this?
I need to be able to use the recipients again later - for example to send them a 'heads up' email about a problem for example.
You need to initialize the array correctly, and you need to use the Address property of a ToRecipient:
var Recipients = new string[email.ToRecipients.Count];
for (int iIdx = 0; iIdx < email.ToRecipients.Count; iIdx++) {
Recipients[iIdx] = email.ToRecipients[iIdx].Address;
}
BTW, I think you have a typo in your pseudo-code:
for(...; iIdx<-email.ToRecipients.Count; ...) {
You have a minus - in there, which would result in no iterations since the first iteration would not pass (0 < -count is false). I think you mean
for(...; iIdx < email.ToRecipients.Count; ...) {
UPDATE
A much simpler, less error-prone, solution would be:
var recipients = email.ToRecipients
.Select(x => x.Address)
.ToList(); // or ToArray()

Adding to a Session ASP.NET C#

I would like to add a Session["i"] with more results
currently it will only shows one set of results
e.g. School1 11/10/2011 14/11/2011 GCSE AAA
I would like to add more sets but they do not seem to be getting stored in the Session
e.g.
School1 11/10/2011 14/11/2011 GCSE AAA
School2 11/10/2012 14/11/2012 ALevels AAA
Education addResults = new Education(schoolName, fromDate, toDate , qualification , grades);
Session["i"] = (addResults );
//schoolarraylist.Add(addResults );
foreach (Education currentschool in schoolarraylist)
{
Session["i"] = currentschool.Schoollocation + "," + currentschool.Datefrom + "," + currentschool.Dateto + "," + currentschool.Qualifications + "," + currentschool.Grade + "<br />";
string tmp = Session["i"].ToString();
string[] sb = tmp.Split(',');
string [] ii = new string[sb.GetUpperBound(0) + 1];
for (int i = 0; i <= sb.GetUpperBound(0); i++)
{
ib[i] = (sb[i]);
}
foreach (string j in ii)
{
Response.Write(ii);
}
}
You can assign list of object to session and later get it back. But you should not put data in seesion without need. Session are maintained on server side for each user and putting data in session takes memory of server and it is could degrade the performance of the application. Its worth reading about sessions before using them.
List<string> lst = new List<string>();
Session["i"] = lst;
Getting list back from session object.
List<string> lst = (List<string>)Session["i"];
The Problem is, that you assign something to Session["i"] and when you try to add something to the session, you actually overwrite your previous value. In order to add objects to the Session, you either have to chose another name e.g. Session["j"] or wrap some sort of container around your objects (List, Array, Dictionary, etc.) and store that container in your Session.
Also try to find better names for your Sessions, if you take a look at your code at a later point, you probably won't know what Session["i"] is actually supposed to be.
You can also use a ArrayList :
ArrayList list = new ArrayList();
foreach (Education currentschool in schoolarraylist)
{
list.Add(currentschool.Schoollocation + "," + currentschool.Datefrom + "," + currentschool.Dateto + "," + currentschool.Qualifications + "," + currentschool.Grade)
}
Then loop through the arraylist and display in whatecver format you want to display

Categories