save smtp sent mail in outlook sent folder using outlook add in - outlook

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();

Related

How to create an evet in other user calendar which I have write permision, using C#?

I am trying to create an application where I add an event in other calendar which I have write access to it.
I was manage to create an application to send an meeting invitation. But I would like to direct add to calendar?
Second step if the schedule was change I would like to change the event date.
Any one could give any idea in how can I create a C# code to add an event in other calendar?
Thanks for any idea!
Tried alredy sending an invitation for a meeting, this worked but it is necessary user approval, them it will not work very well for the application.
using OutLook = Microsoft.Office.Interop.Outlook;
public void NovoAtendimento(MeetInfo nwVisit)
{
OutLook.Application objOL = new OutLook.Application();
OutLook.AppointmentItem objAppt = (OutLook.AppointmentItem)objOL.CreateItem(OutLook.OlItemType.olAppointmentItem);
objAppt.Subject = nwVisit.Titulo;
objAppt.Body = nwVisit.Detalhe;
objAppt.Location = nwVisit.Local;
objAppt.Start = nwVisit.DiaInicio;
objAppt.End = nwVisit.DiaFim;
objAppt.MeetingStatus = OutLook.OlMeetingStatus.olMeeting;
objAppt.RequiredAttendees = nwVisit.Email;
objAppt.Save();
objAppt.Send();
objAppt = null;
objOL = null;
}
Instead of using Application.CreateItem, retrieve the shared folder using Namespace.CreateRecipient / Namespace.GetSharedDefaultFolder, then add an appointment using MAPIFolder.Items.Add.
After checking the hint from Dmitry, this was the final prograns lines that worked:
public void TestCheckCalendar(MeetInfo usrCall)
{
OutLook.Application myApp = new OutLook.Application();
OutLook.NameSpace myNs = myApp.GetNamespace("MAPI");
OutLook.Recipient myReci = myNs.CreateRecipient(usrCall.Email);
OutLook.MAPIFolder calFolder = myApp.Session.GetSharedDefaultFolder(myReci,OutLook.OlDefaultFolders.olFolderCalendar);
OutLook.Items itemTest = null;
OutLook.AppointmentItem appItem = null;
itemTest = calFolder.Items;
appItem = itemTest.Add(OutLook.OlItemType.olAppointmentItem) as OutLook.AppointmentItem;
appItem.Start = usrCall.DiaInicio;
appItem.End = usrCall.DiaFim;
appItem.Location = usrCall.Local;
appItem.Subject = usrCall.Titulo;
appItem.Body = usrCall.Detalhe;
appItem.Save();
}

Automatically map a Contact to an Account

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
}

Yammer "Follow in Inbox" API support

My company has created a Yammer application that we use internally. Our app automatically subscribes people to various threads that have been created. We have found that there is a difference between "subscribing" someone to a thread and what happens when a user clicks the "follow in inbox" link on the site. When we automatically subscribe people, the only thing that we can see happening is that the thread will appear in the users "Following" section in the Home tab. Contrast this with what happens when a user clicks the "Follow in Inbox" link. From that point on any comments added to the thread will show up in the user's inbox and an email will be sent out to the user when this happens. We would really like for this to happen when we automatically subscribe someone to a thread, however, this feature seems to be missing from the REST API. Does anyone know of a way to accomplish this? The functionality provided by the subscription API endpoint is not sufficient for our purposes.
Thank you
P.S. I've sent the link to this question to several of my colleges they may respond before I get a chance to.
As a verified admin it is possible to create an impersonation token and then perform actions on behalf of the user such as join group/thread.
Note that for private groups, the group admin's are still required to approve the new member
https://developer.yammer.com/docs/impersonation
You can achieve your desired behaviour by adding users directly to the groups.
A C#.Net example I use:
// Impersonate user to join group
string ClientID = ConfigurationSettings.AppSettings["ClientID"]; // ClientID of custom app.
string userid = XMLDoc.Root.Element("response").Element("id").Value; // Yammer user id (in this case retreived from a previous API query)
string YammerGroupID = "123456"; // set group id.
string url = "https://www.yammer.com/api/v1/oauth/tokens.json?user_id=" + userid + "&consumer_key=" + ClientID; // impersonation end-point
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization", "Bearer " + bearer); // Bearer token of verified admin running the custom app.
request.Timeout = 90000;
request.Method = "GET";
request.ContentType = "application/json";
request.Proxy = new WebProxy() { UseDefaultCredentials = true };
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream);
string UserTokenJSON = reader.ReadToEnd(); // UserOAuth token as a JSON string.
string UserToken = UserTokenJSON.Substring(UserTokenJSON.IndexOf("token") + 8, 22); // Find 'token' in json string.
string temp = UserToken.Substring(UserToken.Length); // there is likely a much better way to parse out the token value, although this works.
temp = UserToken.Substring(UserToken.Length - 1);
temp = UserToken.Substring(UserToken.Length - 2);
if (UserToken.Substring(UserToken.Length) == "\\")
{ UserToken = UserToken.Substring(0, UserToken.Length); }
if (UserToken.Substring(UserToken.Length - 1) == "\"")
{ UserToken = UserToken.Substring(0, UserToken.Length - 1); }
if (UserToken.Substring(UserToken.Length - 2) == "\",")
{ UserToken = UserToken.Substring(0, UserToken.Length - 2); }
string url2 = "https://www.yammer.com/api/v1/group_memberships.json?group_id=" + YammerGroupID; // group membership endpoint,
HttpWebRequest request2;
request2 = (HttpWebRequest)WebRequest.Create(url2);
request2.Headers.Add("Authorization", "Bearer " + UserToken); // Impersonation Token
request2.Timeout = 90000;
request2.Method = "POST";
request2.ContentType = "application/json";
request2.Proxy = new WebProxy() { UseDefaultCredentials = true };
try
{
using (WebResponse response2 = (HttpWebResponse)request2.GetResponse())
{
confirmedstring += " New member: " + Email + "\\r\\n"; // This is used for posting summary back to a Yammer group in further code.
confirmedadditions++;
}
}
catch
{
Errorstring += "Error in adding " + Email + " to group " + YammerGroupID + "\\r\\n";
errors++;
}
}
}

EWS Managed API : Checking Responses

I'm using EWS Managed API 2.0. I'm using the Calendaring part where you can book appointments as follows :
Appointment appointment = new Appointment(service);
//Set properties on the appointment.
appointment.Subject = "Dentist Appointment";
appointment.Body = "The appointment is with Dr. Smith.";
appointment.Start = new DateTime(2009, 3, 1, 9, 0, 0);
appointment.End = appointment.Start.AddHours(2);
//Save the appointment.
appointment.Save(SendInvitationsMode.SendToNone);
How can I using the API check the status of the booking and whether it was booked or not due to a conflict in the date (Success/Error/Conflict)? right now I'm able to check this through the outlook, but I'd like to know this information from the API. I've looked into the API documentation but I couldn't find anything.
Appreciate your help/guidance.
You should first check the availability of all attendees before saving your appointment. AvailabilityData will return you Result (ServiceResult.Success, ServiceResult.Warning, or ServiceResult.Error) and further you can check ErrorMessage property to find proper return message for each conflicting availability. If availability is not conflicting for any of the attendees, you can save your Appointment object.
AvailabilityOptions availabilityOptions = new AvailabilityOptions();
availabilityOptions.MeetingDuration = 60;
availabilityOptions.MaximumNonWorkHoursSuggestionsPerDay = 4;
availabilityOptions.MinimumSuggestionQuality = SuggestionQuality.Good;
availabilityOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy;
List<AttendeeInfo> attendees = new List<AttendeeInfo>();
attendees.Add(
new AttendeeInfo()
{
SmtpAddress = "org#acme.com",
AttendeeType = MeetingAttendeeType.Organizer
});
attendees.Add(
new AttendeeInfo()
{
SmtpAddress = "at1#acme.com",
AttendeeType = MeetingAttendeeType.Required
});
attendees.Add(
new AttendeeInfo()
{
SmtpAddress = "room1#acme.com",
AttendeeType = MeetingAttendeeType.Room
});
GetUserAvailabilityResults availabilityResults =
service.GetUserAvailability(
attendees,
new TimeWindow(DateTime.Now, DateTime.Now.AddDays(1)),
AvailabilityData.FreeBusyAndSuggestions,
availabilityOptions
);
// Here check the availability Result and ErrorMessage of each attendees
// availabilityResults.AttendeesAvailability[0].Result
// availabilityResults.AttendeesAvailability[0].ErrorMessage
// ServiceResult.Success
// ServiceResult.Warning
// ServiceResult.Error

How to create user properties with EWS or EWS Managed API so that they appear in field chooser

I would like to create User Properties using EWS so that they appear in the field chooser in Outlook. I know it's possible using VBA, the object model exposes an Item.UserProperties collection. However, using EWS I can only access Item.ExtendedProperty.
The issue with ExtendedProperty is that is doesn't appear in the selectable user properties list.
The underlying issue is that our server-side application tries to work nicely together with the Dynamics CRM Outlook Client. The CRM Outlook client uses UserProperty for storing custom properties and our application can only work with EWS and we cannot find a way to create user properties.
Some example code:
User Properties (VBA Outlook):
Dim WithEvents m_objApp As Outlook.AppointmentItem
Private Sub Application_ItemLoad(ByVal Item As Object)
If Item.Class = olAppointment Then
Set m_objApp = Item
End If
End Sub
Private Sub m_objApp_Open(Cancel As Boolean)
Dim oProp1 As UserProperty
Dim oProp2 As UserProperty
If m_objApp.UserProperties.Count = 0 Then
Set oProp1 = m_objApp.UserProperties.Add("crmid", olText)
oProp1.Value = ""
Set oProp2 = m_objApp.UserProperties.Add("crmLinkState", olText)
oProp2.Value = "0"
m_objApp.Save
End If
End Sub
Extended Properties (Exchange EWS):
CalendarItemType item = new CalendarItemType();
item.MeetingTimeZone = new TimeZoneType() { TimeZoneName = _userTimeZone };
item.StartSpecified = true;
item.Start = GetDateFromXml(node.Value);
item.EndSpecified = true;
item.End = GetDateFromXml(node.Value);
List<ExtendedPropertyType> properties = new List<ExtendedPropertyType>();
properties.Add(CreateExtendedProperty("crmid", pending.CrmId.Value.ToString(), MapiPropertyTypeType.String));
properties.Add(CreateExtendedProperty("crmLinkState", "2", MapiPropertyTypeType.Double));
item.ExtendedProperty = properties.ToArray();
CreateRequest createRequest = new CreateItemType()
{
Items = new NonEmptyArrayOfAllItemsType
{
Items = new ItemType[] { item }
},
SavedItemFolderId = new TargetFolderIdType()
{
Item = new DistinguishedFolderIdType()
{
Id = folder,
Mailbox = new EmailAddressType() { EmailAddress = _user.MailBox }
}
},
SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone,
SendMeetingInvitationsSpecified = true
};
CreateItemResponseType response = exchange.CreateItem(createRequest);
private ExtendedPropertyType CreateExtendedProperty(string name, string value, MapiPropertyTypeType type)
{
return new ExtendedPropertyType()
{
ExtendedFieldURI = new PathToExtendedFieldType()
{
PropertyName = name,
DistinguishedPropertySetId = DistinguishedPropertySetType.PublicStrings,
DistinguishedPropertySetIdSpecified = true,
PropertyType = type
},
Item = value
};
}
A similar question has been asked on a Microsoft forum almost a year ago, but no answer yet. http://social.technet.microsoft.com/Forums/en-NZ/exchangesvrdevelopment/thread/c4d6bbb9-ba6a-4aa4-9e39-98a52b733a8c
I was hoping SO would be more successful :)
Thanks,
Jeffry
I thought the two methods were equivalent as long as you used publicstrings (which it looks like you do). How about using MFCMAPI to see the difference in what's generated?

Resources