I'm trying to iterate through attachments in an EmailMessage object.
I'm getting error "attachemnts can't be updated" in this line:
fa.ContentId = fa.Id;
my code is:
EmailMessage emailMsg = EmailMessage.Bind(service, new ItemId(emailId), new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments,ItemSchema.Body,ItemSchema.DateTimeReceived,EmailMessageSchema.DisplayCc,EmailMessageSchema.DisplayTo,EmailMessageSchema.Subject,EmailMessageSchema.From));
this.Email = msgItem;
this.Email.Load();
// make sure email has id or contentid
foreach (Attachment attachment in this.Email.Attachments)
{
FileAttachment fa = attachment as FileAttachment;
if (fa.ContentId == null && fa.Id != null)
{
fa.Load();
fa.ContentId = fa.Id; // error attachments can't be updated
}
}
Thanks
You can't update an Attachment on an existing Exchange Item eg the only valid Attachment operations are Get, Create, Delete there is no Update. The only valid context where you could set the ContentId is when your creating the Message where this property would be set in the CreateAttachment operation. To update an existing attachment you would have to remove it and then re add it as a new attachment. What you actually trying to achieve with your code ?
Cheers
Glen
Related
I want to find out if current Outlook user is a member of particular exchange distribution list. If he is, then he should see child form and if he isn't; then he should see message box.
My following code is working up to the point, if user is a member of DistList, he get child form but I don't know how to check show him message box if he isn't member.
string UserName = (string)application.ActiveExplorer().Session.CurrentUser.Name;
string PersonalPublicFolder = "Public Folders - " + application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
Outlook.MAPIFolder contactsFolder = outlookNameSpace.Folders[PersonalPublicFolder].Folders["Favorites"];
Outlook.DistListItem addressList = contactsFolder.Items["ContactGroup"];
if (addressList.MemberCount != 0)
{
for (int i = 1; i <= addressList.MemberCount; i++)
{
Outlook.Recipient recipient = addressList.GetMember(i);
string contact = recipient.Name;
if (contact == UserName)
{
var assignOwnership = new AssignOwnership();
assignOwnership.Show();
}
}
}
Any help would be appreciated.
Thank you.
Use Application.Session.CurrentUser.AddressEntry.GetExchangeUser().GetMemberOfList() - it will return AddressEntries object that contains all DLs that the user is a member of.
Be prepared to handle nulls and errors.
I want to add a field to Accounts which shows the email domain for that account e.g. #BT.com. I then have a spreadsheet which lists all the Accounts and their email domains. What I want to do is when a new Contact is added to Dynamics that it checks the spreadsheet for the same email domain (obviously without the contacts name in the email) and then assigned the Contact to the Account linked to that domain. Any idea how I would do this. Thanks
Probably best chance would be to develop CRM plugin. Register your plugin to be invoked when on after contact is created or updated (so called post-event phase). And in your plugin update the parentaccountid property of the contact entity to point to account of your choice.
Code-wise it goes something like (disclaimer: not tested):
// IPluginExecutionContext context = null;
// IOrganizationService organizationService = null;
var contact = (Entity)context.InputParameters["Target"];
var email = organizationService.Retrieve("contact", contact.Id, new ColumnSet("emailaddress1")).GetAttributeValue<string>("emailaddress1");
string host;
try
{
var address = new MailAddress(email);
host = address.Host;
}
catch
{
return;
}
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
contact["parentaccountid"] = entities[0].ToEntityReference();
}
organizationService.Update(contact);
I took Ondrej's code and cleaned it up a bit, re-factored for pre-operation. I also updated the logic to only match active account records and moved the query inside the try/catch. I am unfamiliar with the MailAddress object, I personally would just use string mapping logic.
var target = (Entity)context.InputParameters["Target"];
try
{
string host = new MailAddress(target.emailaddress1).Host;
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
query.Criteria.AddCondition("statecode", ConditionOperator.Equals, 0); //Active records only
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
target["parentaccountid"] = entities[0].ToEntityReference();
}
}
catch
{
//Log error
}
I tried below case but getting same error.
microsoft.exchange.webservices.data.core.exception.service.local.ServiceObjectPropertyException: You must load or assign this property before you can read its value.
ExchangeService service = new ExchangeService();
case 1 :
Item itm2 = Item.bind(service, new ItemId(itemId), PropertySet.FirstClassProperties);
EmailMessage emailMessage2 = EmailMessage.bind(service, itm1.getId());
itm2.getMimeContent();
case 2 :
Item itm2 = Item.bind(service, new ItemId(itemId), PropertySet.getFirstClassProperties());
EmailMessage emailMessage2 = EmailMessage.bind(service, itm1.getId());
itm2.getMimeContent();
But getting the same error again and again. I want Mine content so , can change in.eml format .
You need to load the property before you can access it.
Create a PropertySet and add MIMEContent as a property:
PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties);
propSet.Add(ItemSchema.MimeContent);
Then add the PropertySet as an overload property for your Bind code:
EmailMessage emailMessage = EmailMessage.bind(service, itemId, propSet);
String content = emailMessage.getMimeContent().toString();
I would like to send smtp send mail from outlook-addin that mail save into outlook sent folder
Note: Save mail item in sent folder from address must be which i specified smtp from address not outlook login username.
public bool SendEMail()
{
MailMessage mailNew = new MailMessage();
var smtp = new SmtpClient("SmtpServer")
{
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network
};
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("UserName", "password");
smtp.EnableSsl = false;
smtp.Credentials = credentials;
MailAddress mailFrom = new MailAddress("clark#gmail.com");
mailNew.From = mailFrom;
mailNew.To.Add("someone#gmail.com");
mailNew.Subject = Subject;
mailNew.IsBodyHtml = Html;
mailNew.Body = Body;
smtp.Send(mailNew);
return true;
}
thanks in advance
You will need to create a sent item in the Sent Items folder and set all the relevant properties using MailItem.PropertyAccessor. Note that PropertyAccessor will not let you set some sender related properties. In Outlook 2010 or higher you can set the MailItem.Sender property.
Also note that the sent flag cannot be changed after the message is saved, so to create a sent item using OOM, you will need to create a post item, then change its MessageClass property to "IPM.Note".
Also note that ReceivedTime and SentOn properties are reset by OOM every time the message is saved.
If using Redemption is an option (I am its author), you can do something like the following (off the top of my head):
Redemption.RDOSession session = new Redemption.RDOSession();
session.MAPIOBJECT = Application.Session.MAPIOBJECT;
Redemption.RDOMail message = session.GetDefaultFolder(rdoDefaultFolders.olFolderSentMail).Items.Add("IPM.Note");
message.Sent = true;
message.Subject = "test";
message.Body = "fake sent message";
message.Recipients.AddEx("The Recipient", "recipient#domain.com", "SMTP", olTo);
string senderEntryID = session.AddressBook.CreateOneOffEntryID("Some Name", "SMTP", "user#domain.com", false, true);
addressEntry = session.AddressBook.GetAddressEntryFromID(senderEntryID);
message.Sender = addressEntry;
message.SentOnBehalfOf = addressEntry;
message.Save();
I am using VSTO for my Outlook add-in. Currently I am processing email addresses from all Outlook contacts. There is no problem for instances of ContactInfo if EmailAddress1Type is "SMTP".
But how to get email address for Exchange contact (Email1AddressType = "EX")?
Redemption library is not solution for me as it is expensive just to solve this one problem.
Thank you in advance,
Dusan
Here is the MSDN reference link and corresponding sample code:
private const string Email1EntryIdPropertyAccessor = "http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80850102";
PropertyAccessor propertyAccessor = contactItem.PropertyAccessor;
object rawPropertyValue = propertyAccessor.GetProperty(Email1EntryIdPropertyAccessor);
string recipientEntryID = propertyAccessor.BinaryToString(rawPropertyValue);
Recipient recipient = contactItem.Application.Session.GetRecipientFromID(recipientEntryID);
if (null == recipient)
throw new InvalidOperationException();
bool wasResolved = recipient.Resolve();
if (!wasResolved)
throw new InvalidOperationException();
ExchangeUser exchangeUser = recipient.AddressEntry.GetExchangeUser();
string smtpAddress = exchangeUser.PrimarySmtpAddress;