Send email to a user, getting it from a ‘Person or Group’ field November 10, 2009
Posted by rehmangul in Blogroll, MOSS, MS SharePoint Sever, development, event handlers, sharepoint, sps.Tags: Coding SharePoint, Custom Sharepoint, customise, email server, moss customisation, SharePoint Customization
add a comment
First, we have to read from “Person or Group” field.
It could be a “Person or Group” field that allows one user to be selected.
It could be a “Person or Group” field that allows multiple users to be selected.
If its a single user, get user, get his email address, send him email.
If there are multiple users, get each of them one by one, get their email addresses, send them emails.
Do not copy/paste the code; and if you do, look for the typos
using System.Net.Mail;
SPList myList = myWeb.Lists["myListNameHere"];
SPFieldCollection allMyListFields = myList.Fields;
foreach(SPListItem myItem in myList.Items) //go through all the items
{
foreach (SPField myField in allMyListFields) //go through all the fields
{
if (myField.GetType() == typeof(SPFieldUser)) //check if the field is Person or Group field i.e. SPFieldUser
{
Type fieldType = myField.GetFieldValue(myItem[myField].ToString()).GetType();
if (fieldType == typeof(SPFieldUserValueCollection)) //check if the field allows multiple selection of users
{
string emailToFieldValues = myItem[myField].ToString();
SPFieldUserValueCollection emailToUsers = new SPFieldUserValueCollection(myItem.Web, emailToFieldValues.ToString());
foreach (SPFieldUserValue emailUser in emailToUsers)
{
SPUser emailThisUser = emailUser.User;
string emailAddress = emailThisUser.Email.ToString();
SPUtility.SendEmail(myWeb, true, true, emailAddress, emailSubject, emailBody);
}
}
else
{
SPFieldUserValue emailToFieldValue = (SPFieldUserValue)myField.GetFieldValue(myItem[myField].ToString());
SPUser emailToUser = emailToFieldValue.User;
string emailToAddress = emailToUser.Email.ToString();
SPUtility.SendEmail(myWeb, true, true, emailToAddress, emailSubject, emailBody);
}
}
}
}
Hope this helps–
Email enable a sharepoint document library April 9, 2009
Posted by rehmangul in MOSS, MS SharePoint Sever, error, sharepoint, sps.Tags: Custom Sharepoint, customise, document library, email enabled, email enabled document library, email server, Mail exchange, MOSS, moss customisation, moss errors, SharePoint Customization, sharepoint errors, tips and tricks, workarounds
2 comments
To enable a document library to receive emails, first you have to enable this feature in Central Administration -> Operations -> Incoming email settings. Only after that you will see a link in Document Library settings as “Incoming email settings”.
So here is how we do it:
In IIS our default smtp is vanillargk.sydney.wu.local (taking into account my virtual machine, here my machine name is vanillargk and local domain is sydney.wu.local, domain could be seen in IIS). Mail Server is specified in Central Admin -> Operations -> Incoming email settings…….Select “Yes” for “Enable sites on this server to receive email?” radio button. For “Directory Management Service” you may leave it as “No”. For “Email Server Display Address” put your “mail server”, in my case it would be vanillargk.sydney.wu.local. Select any mail drop folder, or let it be as default “c:\inetpub\mailroot\drop”.
Now go to doc lib settings -> Incoming email settings and in Email Address field just give email address like “checkmail” or anything that you may like………….”@vanillagrk.sydney.wu.local” will be already there as readonly…………..so your email address will become checkmail@vanillargk.sydney.uw.local. Make sure you select “Yes” for the option saying ”Save original e-mail?”……the default value is probably set as “No” which will not let email messages appear in the doc lib. For the time being select “Accept e-mail messages from any sender” in “Email security policy” option. Once its working, you can select “Accept e-mail messages based on doc lib permissions” option to comply with doc lib security settings.
So now how do we receive mail? For a server or a machine to receive emails, it has to have two things: a DNS entry and an SMTP entry. Such that when an email comes from somewhere outside, it is routed to the specific machine (using DNS) and that machine is known to everyone as a Mail Server (using SMTP entry). DNS entry is obviously made in the DNS Server. The domain controller running Active Directory has a Mail Exchanger (MX) entry in DNS Manager for the Mail Server that you plan to use for incoming e-mail. So, in our case both of these entries should be made using vanillargk.sydney.wu.local.
These entries are made by the Infrastructure guys, dont worry if you dont know how to do it, just ask your IT team to make these entries for you.
This is how it will be done by the way:
Add a Mail Exchanger (MX) resource record for the subdomain In DNS Manager, select the forward lookup zone for the domain that contains the subdomain for Office SharePoint Server 2007. Right-click the zone, and then click New Mail Exchanger. In the Host or domain text box, type the host or subdomain name for Office SharePoint Server 2007. In the Fully qualified domain name (FQDN) of mail server text box, type the fully qualified domain name for the server that is running Office SharePoint Server 2007. This is typically in the format subdomain.domain.com. Click OK.
Remember, SMTP service should be enabled on your machine which you intend to use as a mail server. In our case it is the machine on which our doc libs reside i.e. sharepoint server. This is how you do it:
- Start Control Panel Add/Remove Programs.
- Click Add/Remove Windows Components.
- After the Windows Components Wizard appears, select Applications Server and click Details.
- Select Internet Information Services (IIS), then click Details.
- Select SMTP Service, then click OK.
- Continue to click OK to close all other dialog boxes until you’re back at the Windows Components Wizard page, then click Next.
After all this, your doc lib is ready to receive emails. Try and let me know
.
Hope this helps…..



