I'm currently using the Microsoft.Azure.ActiveDirectory.GraphClient in a C# Cloud Service application that manages user provisioning and updates to users in Office 365 based on information stored in a database.
The issue I am facing is the setting of a user's mail address. On provisioning of a new user, setting the mail address property of the new user object causes an error to be thrown by the Graph Client. When provisioning a new user, the primary SMTP address is automatically set to the be the UPN for the user. The following code will successfully create a new user and set the primary SMTP address to be the same as the value set for the UserPrincipalName:
IUser newUser = new User();
newUser.DisplayName = "Firstname Surname";
newUser.UserPrincipalName = "someone#somewhere.com";
newUser.AccountEnabled = true;
newUser.MailNickname = "firstnamesurname";
newUser.ImmutableId = "0k3otwAAEkm8vGSKbJqRZg==";
newUser.PasswordProfile = new PasswordProfile
{
Password = "somerandompassword",
ForceChangePasswordNextLogin = true
};
newUser.UsageLocation = "GB";
_activeDirectoryClient.Users.AddUserAsync(newUser).Wait();
Fair enough - having UPN and primary email address mis-matches can cause AutoDiscover issues.
The problem I have is that the "mail" property of the user object also throws an error when set when updating a user.
The following code will successfully update a user:
User retrievedUser = new User();
List<IUser> retrievedUsers = null;
retrievedUsers = _activeDirectoryClient.Users
.Where(searchUser => searchUser.ImmutableId.Equals(0k3otwAAEkm8vGSKbJqRZg==))
.ExecuteAsync().Result.CurrentPage.ToList();
if (retrievedUsers != null && retrievedUsers.Count == 1)
{
retrievedUser = (User)retrievedUsers.First();
retrievedUser.UserPrincipalName = "someone1#somewhere.com";
retrievedUser.UpdateAsync().Wait();
}
The above code will change the user's UPN, but not update the primary SMTP address as in the add a new user scenario. This I do not understand, as it can then cause AutoDiscover issues (as the UPN is different to the primary SMTP address) and negate any reason for not being able to set the mail address when creating a new user.
I cannot find any details on how to update a user's email address, or set an additional email address as the primary SMTP address. Scenarios for this requirement are such as when a user gets married and they want to have a new email address as their primary email address.
Does anyone have any info please on how to manage a user's primary SMTP address using Microsoft.Azure.ActiveDirectory.GraphClient? I can find information on setting additional email addresses, but not on how to change the primary SMTP address.
Grateful for any help please!
From testing this myself, an update to the userPrincipalName through the AzureAD Graph API automatically updates the following properties:
mail
userPrincipalName
proxyAddresses
With the proxyAddresses property being updated to include the new userPrincipalName as the new primary SMTP address, and the old primary address is preserved but no longer the primary address. This is also reflected in Exchange Online. The proxyAddress which is prefixed with SMTP: (uppercase) is the PrimarySmtpAddress.
As an edge case, this process does not happen if the previous primary address is not the same as the previous UPN. If the UPN and PrimarySmtpAddress do not match, then when the UPN is updated through the AzureAD Graph API then the new UPN will be added as a non-primary address, and the original primary address will remain the primary address.
As far as I know, this behavior is undocumented. However, something similar is documented here: https://support.microsoft.com/en-us/help/3190357/how-the-proxyaddresses-attribute-is-populated-in-azure-ad. It seems the priority of which property determines the PrimarySmtpAddress goes mail > UPN > mailNickName.
In general, I don't think it's a good idea to directly manipulate proxyAddresses because Exchange/AD already do a lot of this for you.
Following a random thought, I've figured out how to change the primary email address for a user in the above scenario. This is done by setting the primary email address in the proxy address list for the user, denoting the primary address with the prefix of "SMTP:" (note it has to be in CAPS):
retrievedUser.ProxyAddresses = new List<string>
{
"SMTP: someone1#somewhere.com"
};
Hope this helps someone else!
Related
I am trying to create a User in Azure AD with Microsoft Graph Client. I am able to create the User but I couldn't find a property for the Email or Alternative Email of the User.
Here is the code:
await client.Users.Request()
.AddAsync(new User()
{
AccountEnabled = true,
DisplayName = displayName,
PasswordProfile = new PasswordProfile()
{
ForceChangePasswordNextSignIn = true,
Password = "P#ssword123"
},
UserPrincipalName = $"{displayName}#{domain}",
MailNickname = displayName,
});
The User's primary email address should also be the userPrincipalName.
That said, the properties you're looking for are mail and The property is mail and proxyAddresses (this is an array of alternative SMTP addresses). These properties, however, are read-only in Microsoft Graph at the moment.
There is an open User Voice for this: Ability to update the user's email aliases (proxyAddresses attribute). It could certainly use more votes (hint hint).
In order to populate these fields, you may need to use the Azure AD Graph API. The User object in this API includes support for writing to mail and otherMails (it also has proxyAddresses but that collection is also read-only).
I am trying to send message from server email to my gmail
WebMail.SmtpServer = "mail.marekjedrysiak.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = true;
WebMail.UserName = "mail#marekjedrysiak.com";
WebMail.Password = "******";
WebMail.From = "mail#marekjedrysiak.com";
WebMail.Send("marekjedrysiak1#gmail.com", "Message from server",null);
then coming this message :
Please help :(
This isn't an ASP.NET or C# related problem, your domain doesn't have an MX record. You can confirm this by checking http://www.mxtoolbox.com.
I see that your domain is registered with Tucows (Hover). In order to change your MX records:
Log in to your Hover account. If you have multiple domain names, select your marekjedrysiak.com domain.
Click the DNS tab.
Click Add New.
For Record Type, select MX.
Enter the address of your mail server.
Click Save.
If you need more help setting up or debugging your mail service, you should contact Hover's support. They can also advise you on what exactly to enter as the address of your mail server.
Source:
https://help.hover.com/hc/en-us/articles/217282457-How-to-Edit-DNS-records-A-CNAME-MX-TXT-and-SRV-Updated-Aug-2015-
I have this code in order to get the email
string code = Request.QueryString["code"];
if (!string.IsNullOrEmpty(code)){
string data = FaceBookConnect.Fetch(code, "me");
FaceBookUser faceBookUser = new JavaScriptSerializer().Deserialize<FaceBookUser>(data);
And on the click event I defined this:
FaceBookConnect.Authorize("email", Request.Url.AbsoluteUri.Split('?')[0]);
The problem is that it gives me a null value as email, i tried doing the same for the name and it is the only field that actually works. I read that thay have changed the version to v2.6 and it is necessary to do the facebookConnect.Fetch in another way, but I am unable to find how. Anyone knows something? Thanks everyone!
Have you requested the email info from Facebook? You must do that at start of login and the user has to approve this before Facebook will provide it. Facebook also says it will be null if the address is not valid or if they used a phone number to sign up:
"Note, even if you request the email permission it is not guaranteed you will get an email address. For example, if someone signed up for Facebook with a phone number instead of an email address, the email field may be empty."
https://developers.facebook.com/docs/facebook-login/permissions#reference-email
Is there a way to get the primary SMTP address from an exchange account in Outlook ? I am currently using Redemption and following code keeps returning an empty value.
> IRDOExchangeAccount exAcc = (IRDOExchangeAccount)rdoAcc;
> string email = exchangeAccount.CurrentUser.SMTPAddress;
Currently I am using
exchangeAccount.CurrentUser.Address
property to get the Email and seems it gets me the correct Email address. How reliable that property is ? Is there another better way ?
I want to use the email address that is stored in a SQL Table (Memberships), the one created by Visual Studio when you build a website. NB: Not an MVC Site, just a regular webform
Say for instance, If i am logged into the website, with my username and password --> it recognises me as the user X. Bearing in mind user X has a user name, password, email address.
Now, during the application i want to press a submit button which sends an email BUT i want it to use user X's email address as the FROM address.
Part of submit button:
MailAddress to = new MailAddress(nameddl.Text);
MailAddress from = new MailAddress("User X's email address");
MailMessage message = new MailMessage(from, to);
Example of SQL Table:
ID -- Username -- Email Address -- Password
1 -- UserX -- userx#x.com -- password
I am struggling to find a way to insert a variable or something that contain the logged in user's email address in this line without manually typing in their address -->
MailAddress from = new MailAddress("User X's email address");
Can anyone show me a way to do it please?
You should be able to do something like this:
var currentUser = Membership.GetUser(User.Identity.Name);
var userEmail = currentUser.Email;
See Membership.GetUser
MailAddress from = new MailAddress(userEmail);
Seems to be work for sessions ;)
http://msdn.microsoft.com/en-us/library/vstudio/ms178581(v=vs.100).aspx
Session["mail"] = new MailAddress("...");