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| Applicant Name: | | {0} | |
", item["Applicant Name"]);
SPFieldLookupValue jobValue = (SPFieldLookupValue)item.Fields["Job"].GetFieldValue((string)item["Job"]);
body.AppendFormat("\r\n| Job: | | {0} | |
", jobValue.LookupValue);
body.AppendFormat("\r\n| Applied Date: | | {0} | |
", item["Applied Date"]);
body.AppendFormat("\r\n| Email Address: | | {0} | |
", item["Email Address"]);
body.AppendFormat("\r\n| Phone Number: | | {0} | |
", item["Phone Number"]);
body.AppendFormat("\r\n| Notes: | | {0} | |
", item["Notes"]);
body.Append("
");
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