protected void bt_SendMail_Click(object sender, EventArgs e)
{
string result = sendSalaryMail();
lbResult.Text = result;
}
private string sendSalaryMail()
{
#region varian
StringBuilder sbResult = new StringBuilder();
string tmpId = tb_TempId.Text;
string emailFrom = string.Empty;
string emailTitle = string.Empty;
string emailBody = string.Empty;
string folderPath = string.Empty;
string filePath = string.Empty;
int iSuccess = 0;
int iTotal = 0;
sbResult.Append(System.DateTime.Now.ToString());
//Set value
if (string.IsNullOrEmpty(tmpId))
{
tmpId = "SALARY";
}
SPListItem mailTemp = getItemByTempID(tmpId);
if (mailTemp == null)
{
sbResult.Append("<div style='color:red'>Cannot get the mail template '" + tmpId + "'.</div>");
return sbResult.ToString();
}
//set file address
folderPath = @"E:\Batter\TFS\S\";
emailTitle=Convert.ToString(mailTemp["EmailTitle"]);
emailFrom = Convert.ToString(mailTemp["MailFrom"]);
emailBody=Convert.ToString(mailTemp["MailBody"]);
#endregion
try
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["UserList"];
SPListItemCollection items = list.Items;
iTotal = items.Count;
foreach (SPListItem item in items)
{
string mailTo = Convert.ToString(item["Email"]);
string var1 = Convert.ToString(item["Var1"]);
string var2 = Convert.ToString(item["Var2"]);
string var3 = Convert.ToString(item["Var3"]);
string htmlBodyContent = emailBody.Replace("{Var1}", var1).Replace("{Var2}", var2).Replace("{Var3}", var3);
filePath = folderPath + "xxx"+var1+".doc";
using (var message = new MailMessage())
{
SPWebApplication webApp = web.Site.WebApplication;
message.Subject = emailTitle;
message.From = new MailAddress(emailFrom);
message.To.Add(mailTo);
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
try
{
Attachment myAttachment = new Attachment(filePath);
message.Attachments.Add(myAttachment);
}
catch (Exception ex)
{
sbResult.Append("<div style='color:Red'>To: " + mailTo + " Error! " + ex.Message + "</div><br />");
continue;
}
message.AlternateViews.Add(htmlBody);
var smtpClient = new SmtpClient
{
Host =
webApp.OutboundMailServiceInstance.Server.Name,
UseDefaultCredentials = true,
DeliveryMethod = SmtpDeliveryMethod.Network
};
try
{
smtpClient.Send(message);
sbResult.Append("<div style='color:blue'>To: " + mailTo + " Success!</div>");
iSuccess++;
}
catch (Exception ex)
{
sbResult.Append("<div style='color:Red'>To: " + mailTo + " Error! " + ex.Message + "</div><br />");
}
}
}
}
}
}
catch (Exception ex)
{
sbResult.Append(ex.Message);
}
sbResult.Append(string.Format("Success: {0} Total: {1}<br />",iSuccess,iTotal));
sbResult.Append(System.DateTime.Now.ToString());
return sbResult.ToString(); ;
}
private SPListItem getItemByTempID(string tempId)
{
SPListItem item = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Template"];
if (list != null)
{
SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">{0}</Value></Eq></Where>", tempId);
query.RowLimit = 1;
SPListItemCollection items = list.GetItems(query);
if (items != null)
{
if (items.Count == 1)
{
item = items[0];
}
}
}
web.AllowUnsafeUpdates = false;
}
}
});
return item;
}
}
<style type="text/css">
.sendSalaryMail {
margin-left: 20px;
}
.trainingtitle {
color: rgb(125, 10, 60);
font-size: 16px;
margin-bottom: -10px;
font-weight: bold;
margin-bottom: 5px;
}
.colTitle {
font-weight: bold;
color: Black;
}
</style>
<div class="sendSalaryMail">
<div class="trainingtitle">Send E-Mail <b>-- Only xxx can click!! </b></div>
<br />
<div class="colTitle">Email Template ID: </div>
<asp:TextBox ID="tb_TempId" runat="server" Width="340px"></asp:TextBox><div>Defalut is "xxx"</div>
<br />
<div class="colTitle">Has File: <asp:CheckBox ID="cb_HasFile" runat="server" Text="Yes" /></div>
<br />
<asp:Button ID="bt_SendMail" runat="server" Text="Send xx xx" Height="50px"
Width="350px" OnClick="bt_SendMail_Click" /><br />
<div style="margin-top: 10px;">
<asp:UpdatePanel runat="server" ID="UpdatedPanel1" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="bt_SendMail" />
</Triggers>
<ContentTemplate>
<fieldset style="border: none; padding: 0px">
<asp:Label ID="lbResult" runat="server" ForeColor="#0000CC" Font-Size="Medium"></asp:Label>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>