<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SendMailWithAttachment._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Send Mail Using Gmail Account With Attachments</title>
</head> <body>
<form id="Form1" method="post" runat="server">
<table borderColor="#339933" cellSpacing="0" cellPadding="4" width="55%" align="center" border="1">
<tr> <td>
<table cellPadding="4" width="70%" align="center" border="0">
<tr bgColor="#339933"> <td align="center" colSpan="2"><asp:label id="lblHeader" Runat="server" Font-Bold="True">Gmail Account Details for Sending Mails</asp:label></td> </tr>
<tr> <td vAlign="middle" align="right" width="40%">Username:</td> <td vAlign="middle" width="60%"> <asp:TextBox ID="txtUserName" runat="server" Font-Names="Verdana" Font-Size="X-Small"
Width="350px"></asp:TextBox>
</td>
</tr>
<tr>
<td vAlign="middle" align="right" width="40%">Password</td>
<td vAlign="middle" width="60%">
<asp:TextBox ID="txtPassword" Font-Names="Verdana" Font-Size="X-Small"
Width="350px" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr bgColor="#339933">
<td align="center" colSpan="2"><asp:label id="Label1" Runat="server" Font-Bold="True">SMTP Mail with Attachment</asp:label></td>
</tr>
<tr>
<td vAlign="middle" align="right" width="40%">From :</td>
<td vAlign="middle" width="60%"><asp:textbox id="txtSender" tabIndex="1" runat="server" Font-Names="Verdana" Font-Size="X-Small"
Width="350px"></asp:textbox></td>
</tr>
<tr>
<td vAlign="middle" align="right">To :</td>
<td><asp:textbox id="txtReceiver" tabIndex="1" runat="server" Font-Names="Verdana"
Font-Size="X-Small" Width="350px"></asp:textbox></td>
</tr>
<tr>
<td vAlign="middle" align="right">Cc :</td>
<td><asp:textbox id="txtCc" tabIndex="1" runat="server" Font-Names="Verdana" Font-Size="X-Small"
Width="350px"></asp:textbox></td>
</tr>
<tr>
<td vAlign="middle" align="right">Bcc :</td>
<td><asp:textbox id="txtBcc" tabIndex="1" runat="server" Font-Names="Verdana" Font-Size="X-Small"
Width="350px"></asp:textbox></td>
</tr>
<tr>
<td vAlign="middle" align="right">Subject :</td>
<td><asp:textbox id="txtSubject" tabIndex="2" runat="server" Font-Names="Verdana"
Font-Size="X-Small" Width="350px"></asp:textbox></td>
</tr>
<tr>
<td vAlign="middle" align="right">Format :</td>
<td><asp:radiobuttonlist id="rblMailFormat" tabIndex="3" runat="server" repeatcolumns="2" repeatdirection="Horizontal">
<asp:ListItem Value="Text" Selected="True">Text</asp:ListItem>
<asp:ListItem Value="HTML">HTML</asp:ListItem>
</asp:radiobuttonlist></td>
</tr>
<tr>
<td vAlign="middle" align="right">Message :</td>
<td height="84">
<p><asp:textbox id="txtBody" tabIndex="4" runat="server" Font-Names="Verdana" Font-Size="X-Small"
columns="40" rows="5" textmode="MultiLine" width="350px"></asp:textbox></p>
</td>
</tr>
<tr>
<td vAlign="middle" align="right">Attachment :</td><td><input id="inpAttachment1" tabIndex="5" type="file" size="53" name="filMyFile" runat="server"></td> </tr>
<tr>
<td vAlign="middle" align="right">Attachment :</td>
<td><input id="inpAttachment2" tabIndex="6" type="file" size="53" name="filMyFile" runat="server"></td>
</tr>
<tr>
<td vAlign="middle" align="right">Attachment :</td>
<td><input id="inpAttachment3" tabIndex="7" type="file" size="53" name="filMyFile" runat="server"></td>
</tr>
<tr>
<td align="center" colSpan="2"><asp:button id="btnSend" tabIndex="9" runat="server" width="100px" text="Send" onclick="btnSend_Click"></asp:button></td>
</tr>
<tr>
<td align="center" colSpan="2"><asp:Label ID="lblMessage" Runat="server"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
using System.Net.Mail;
using System.IO;
using System.Drawing;
using System;
using System.Drawing;
using System.IO;
using System.Net.Mail;
using System.Web;
namespace SendMailWithAttachment
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
/* Create a new blank MailMessage with the FROM and TO addresses*/
MailMessage mailMessage = new MailMessage(txtSender.Text,txtReceiver.Text);
/* Checking the condition that the cc is empty or not.
If not then include them */
if (txtCc.Text != null && txtCc.Text != string.Empty)
{
mailMessage.CC.Add(txtCc.Text);
}
/* Checking the condition that the Bcc is empty or not.
If not then include them */
if (txtBcc.Text != null && txtBcc.Text != string.Empty)
{
mailMessage.Bcc.Add(txtBcc.Text);
}
//Adding Subject to the Mail
mailMessage.Subject = txtSubject.Text;
//Adding the Mail Body
mailMessage.Body = txtBody.Text;
/* Set the properties of the MailMessage to the values
on the form as per the mail is HTML formatted or plain text */
if (rblMailFormat.SelectedItem.Text == "Text")
mailMessage.IsBodyHtml = false;
else
mailMessage.IsBodyHtml = true;
/* We use the following variables to keep track of
attachments and after we can delete them */
string attach1 = null;
string attach2 = null;
string attach3 = null;
/* strFileName has a attachment file name for
attachment process. */
string strFileName = null;
/* Begining of Attachment1 process &
check the first open file dialog for a attachment */
if (inpAttachment1.PostedFile != null)
{
/* Get a reference to PostedFile object */
HttpPostedFile attFile = inpAttachment1.PostedFile;
/* Get size of the file */
int attachFileLength = attFile.ContentLength;
/* Make sure the size of the file is > 0 */
if (attachFileLength > 0)
{
/* Get the file name */
strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);
/* Save the file on the server */
inpAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName));
/* Create the email attachment with the uploaded file */
Attachment attach = new Attachment(Server.MapPath(strFileName));
/* Attach the newly created email attachment */
mailMessage.Attachments.Add(attach);
/* Store the attach filename so we can delete it later */
attach1 = strFileName;
}
}
/* Attachment-2 Repeat previous step defiend above*/
if (inpAttachment2.PostedFile != null)
{
HttpPostedFile attFile = inpAttachment2.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
strFileName = Path.GetFileName(inpAttachment2.PostedFile.FileName);
inpAttachment2.PostedFile.SaveAs(Server.MapPath(strFileName));
Attachment attach = new Attachment(Server.MapPath(strFileName));
mailMessage.Attachments.Add(attach);
attach2 = strFileName;
}
}
/* Attachment-3 Repeat previous steps step defined above */
if (inpAttachment3.PostedFile != null)
{
HttpPostedFile attFile = inpAttachment3.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
strFileName = Path.GetFileName(inpAttachment3.PostedFile.FileName);
inpAttachment3.PostedFile.SaveAs(Server.MapPath(strFileName));
Attachment attach = new Attachment(Server.MapPath(strFileName));
mailMessage.Attachments.Add(attach);
attach3 = strFileName;
}
}
/* Set the SMTP server and send the email with attachment */
SmtpClient smtpClient = new SmtpClient();
// smtpClient.Host = emailServerInfo.MailServerIP;
//this will be the host in case of gamil and it varies from the service provider
smtpClient.Host = "smtp.gmail.com";
//smtpClient.Port = Convert.ToInt32(emailServerInfo.MailServerPortNumber);
//this will be the port in case of gamil for dotnet and it varies from the service provider
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
//smtpClient.Credentials = new System.Net.NetworkCredential(emailServerInfo.MailServerUserName, emailServerInfo.MailServerPassword);
smtpClient.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
//this will be the true in case of gamil and it varies from the service provider
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
/* Delete the attachements if any */
try
{
if (attach1 != null)
File.Delete(Server.MapPath(attach1));
if (attach2 != null)
File.Delete(Server.MapPath(attach2));
if (attach3 != null)
File.Delete(Server.MapPath(attach3));
}
catch{}
/* clear the controls */
txtSender.Text = string.Empty;
txtReceiver.Text = string.Empty;
txtCc.Text = string.Empty;
txtBcc.Text = string.Empty;
txtSubject.Text = string.Empty;
txtBody.Text = string.Empty;
txtUserName.Text = string.Empty;
/* Dispaly a confirmation message to the user. */
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Black;
lblMessage.Text = "Message sent.";
}
catch (Exception ex)
{
/* Print a message informing the
user about the exception that was risen */
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Red;
lblMessage.Text = ex.ToString();
}
}
} // partial class _Default
} // namespace SendMailWithAttachment
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)