In this article I’m
going to explain about sending mails in asp.net SharePoint 2013 application with attachments..
Design UI as shown in
below figure with validations.
.aspx code:-
<table ><tr> <td
><strong>To:</strong></td>
<td > <asp:TextBox ID="txtto"
runat="server" Width="310px"
/> </td>
</tr>
<tr><td ><strong>Cc:</strong></td>
<td ><asp:TextBox ID="txtcc"
runat="server"
Width="310px"/>
<asp:RegularExpressionValidator
ID="regexcc" runat="server"
ControlToValidate="txtcc" ErrorMessage="Enter a valid
Email" Style="color: #FF0000"
ValidationExpression="^((\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\s*[;]{0,1}\s*)+$"
ValidationGroup="MailValidation"/></td><tr>
<tr><td ><strong>Bcc:</strong></td>
<td > <asp:TextBox ID="txtbcc"
runat="server" Width="310px"/>
<asp:RegularExpressionValidator
ID="gerexbcc" runat="server"
ControlToValidate="txtbcc" ErrorMessage="Enter a valid
Email" Style="color: #FF0000" ValidationExpression="^((\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\s*[;]{0,1}\s*)+$"
ValidationGroup="MailValidation"/></td></tr>
<tr><td ><strong>Subject:<strong style="color:
#FF0000">*</strong></strong></td>
<td ><asp:TextBox ID="txtsubject"
runat="server" Width="310px"/>
<asp:RequiredFieldValidator
ID="rfvsubject" runat="server"
ControlToValidate="txtsubject" ErrorMessage="Please Enter
Subject" ForeColor="Red" ValidationGroup="MailValidation"/></td></tr>
<tr><td
><strong>Body:<strong style="color:
#FF0000">*</strong></strong></td>
<td ><asp:TextBox ID="txtbody"
runat="server" TextMode="MultiLine" Rows="10" />
<asp:RequiredFieldValidator
ID="rfvbody" runat="server"
ControlToValidate="txtbody" ErrorMessage="Please Enter
Body" ForeColor="Red" ValidationGroup="MailValidation"
/> </td> </tr>
<tr> <td ><strong>Attachments<strong
style="color: #FF0000">*</strong></strong></td>
<td >
<asp:FileUpload
ID="FileUpAttachment" runat="server"
AllowMultiple="true" />
</td>
</tr>
<tr> <td
class="auto-style11"> </td>
<td> <asp:Button ID="btnsend" runat="server"
BackColor="#066093" Font-Bold="True"
ForeColor="White" Text="Send"
ValidationGroup="MailValidation" Height="31px"
Width="78px" OnClick="btnsend_Click" /> </td>
</tr>
</table>
<asp:Label ID="lblResult" runat="server"
></asp:Label>
In this .aspx code
RegularExpression Validation control is for multiple mails means it will allow
multiple mails with ‘;’ separation.
ValidationExpression="^((\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\s*[;]{0,1}\s*)+$"
Fill the necessary data
and click on “Send” button to send mail like below.
To send mail through
SharePoint application we need to use MailMessage class like below.
MailMessage message = new MailMessage();
//Get the Sharepoint SMTP information
from the
//SPAdministrationWebApplication
message.From = new
MailAddress(SPAdministrationWebApplication.Local.OutboundMailSenderAddress.ToString());
message.To.Add(new
MailAddress(txtto.Text));
message.IsBodyHtml = true;
message.CC.Add(new
MailAddress(txtcc.Text));
message.Bcc.Add(new
MailAddress(txtbcc.Text));
}
//Set the subject and of
the message
message.Body = txtbody.Text;
message.Subject = txtsubject.Text;
message.Attachments.Add(new
Attachment(attchMent.InputStream,attchMent.FileName));
//Create the SMTP client
object and send the message
SmtpClient smtpClient = new
SmtpClient(SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address);
smtpClient.Send(message);
Thank you....!

