07 November 2008 4:33 PM Posted by Mano Mangaldas

SPUtility.SendEmail.. can be used most of the time to send email. But when you want to send email with attachments.. //you can use SmtpClient from System.Net.Mail namespace.
using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

using System.Net.Mail;

using Microsoft.SharePoint.Administration;

 

namespace HRApplication

{

    public class ApplicantEventReceiver : SPItemEventReceiver

    {

        public override void ItemAdded(SPItemEventProperties properties)

        {

            SPListItem item = properties.ListItem;

            SmtpClient client = LoadSmtpInformation();

            client.Send(BuildMailMessage(item));

        }

 

        private string BuildEmailBody(SPListItem item)

        {

            StringBuilder body = new StringBuilder();

 

            body.Append("This email has been automatically sent from the Hiring Management system. 

"); body.Append(""); body.Append(""); body.AppendFormat("\r\n", item["Applicant Name"]); SPFieldLookupValue jobValue = (SPFieldLookupValue)item.Fields["Job"].GetFieldValue((string)item["Job"]); body.AppendFormat("\r\n", jobValue.LookupValue); body.AppendFormat("\r\n", item["Applied Date"]); body.AppendFormat("\r\n", item["Email Address"]); body.AppendFormat("\r\n", item["Phone Number"]); body.AppendFormat("\r\n", item["Notes"]); body.Append("
Applicant Name:{0}
Job:{0}
Applied Date:{0}
Email Address:{0}
Phone Number:{0}
Notes:{0}
"); return body.ToString(); } private SmtpClient LoadSmtpInformation() { string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address; SmtpClient client = new SmtpClient(smtpServer); client.Credentials = new System.Net.NetworkCredential("mmangaldas", ""); return client; } private string LoadHiringManagersEmail(SPListItem item) { string HRMgrEmail = string.Empty; try { SPFieldLookupValue jobValue = (SPFieldLookupValue)item.Fields["Job"].GetFieldValue((string)item["Job"]); SPList jobList = item.ParentList.ParentWeb.Lists["Jobs"]; SPListItem jobItem = jobList.Items.GetItemById(jobValue.LookupId); SPList departmentList = jobList.ParentWeb.Lists["Departments"]; SPFieldLookupValue departmentValue = (SPFieldLookupValue)jobItem.Fields["Department"].GetFieldValue((string)jobItem["Department"]); SPItem departmentItem = departmentList.Items.GetItemById(departmentValue.LookupId); SPFieldUserValue hiringManager = (SPFieldUserValue)departmentItem.Fields["Hiring Manager"].GetFieldValue((string)departmentItem["Hiring Manager"]); HRMgrEmail = hiringManager.User.Email; } catch (Exception ex) { string s = ex.Message; } return HRMgrEmail; } private MailMessage BuildMailMessage(SPListItem item) { MailMessage message = new MailMessage(); SPList list = item.ParentList; message.From = new MailAddress(SPAdministrationWebApplication.Local.OutboundMailSenderAddress.ToString()); message.To.Add(new MailAddress(LoadHiringManagersEmail(item))); message.IsBodyHtml = true; message.Body = BuildEmailBody(item); message.Subject = "Hiring Management - Applicant Created"; for (int attachmentIndex = 0; attachmentIndex < item.Attachments.Count; attachmentIndex++) { string url = item.Attachments.UrlPrefix + item.Attachments[attachmentIndex]; SPFile file = list.ParentWeb.GetFile(url); message.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name)); } return message; } } }

Comments (0)

Post a Comment