Create a Send Email with Attachment form
<table><tr><td colspan="2"><h3>Send Email with Attachment in ASP.NET</h3></td></tr><tr><td>To</td><td><asp:TextBox ID="txtTo" runat="server" /></td></tr><tr><td>Subject</td><td><asp:TextBox ID="txtSubject" runat="server" Columns="30" /></td></tr><tr><td>Body</td><td><asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Rows="10" Columns="50" /></td></tr><tr style="font-weight:bold;"><td>Attachment</td><td><asp:FileUpload ID="FileUpload1" runat="server" /></td></tr><tr><td> </td><td><asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="SendEmailWithAttachment" /></td></tr></table><asp:Label ID="lblMessage" runat="server" EnableViewState="false" />
This code snippet shall create a Send Email form that looks like above picture. Notice that on click on the button I have attached a server side click event that will fire SendEmailWithAttachment method, so let's write code for that.
protected void SendEmailWithAttachment(object sender, EventArgs e) {
SendEmail(txtTo.Text.Trim(), "", "", txtSubject.Text.Trim(), txtBody.Text.Trim(), MailPriority.High, false); }
In the above code, I have fired a SendEmail private method that will actually do the work of sending email. Below is the code for the SendEmail method.
Namespace to use
To work with SendEmail method code, you need to use following namespaces
using System.IO; using System.Net; using System.Net.Mail;
SendEmail method code
private void SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml) {
try
{
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress("fromEmail@MyDomain.com", "FromEmail, myDomain.Com");
// You can specify the host name or ipaddress of your server
smtpClient.Host = "smtp.gmail.com"; //you can also specify mail server IP address here
//Gmail Port
smtpClient.Port = 587;
NetworkCredential info = new NetworkCredential("fromEmail@myDomain.com", "fromEmailPassword");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;
//From address will be given as a MailAddress Object
message.From = fromAddress; message.Priority = priority;
// To address collection of MailAddress
message.To.Add(toAddress); message.Subject = subject;if (ccAddress.Length > 0)
{
message.CC.Add(ccAddress);
}
if (bccAddress.Length > 0)
{
message.Bcc.Add(bccAddress);
}
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = isHtml;
// Message body content
message.Body = body;
// Add the attachment, if any
if (FileUpload1.PostedFile.ContentLength > 0)
{
Attachment attachment = new Attachment(Path.GetFullPath(FileUpload1.PostedFile.FileName));
message.Attachments.Add(attachment);
}
// Send SMTP mail
smtpClient.Send(message);
lblMessage.Text = "Email sent to " + toAddress + " successfully !";
}
}
}
catch (Exception ee)
{
lblMessage.Text = ee.ToString();
} }
In the above code snippet, I have instantiated the SmtpClient object that provides ability to send email in ASP.NET. After that I have instantiated the MailMessage object that will have necessary properties set to be used to send email (for example, to, from, network body attachment etc.). All the above procedures are same as we do for normal sending email (like setting the network credential for the SmtpClient object, specifying host name etc.)
Important lines of code to notice in the above method is related with the file attachment, notice the if condition block just above smtpClient.Send(message) line. In this if block I have first checked if any file has been selected by accessing theContentLength property of the FileUpload control; if yes then I have instantiated the Attachment object and passed the path of the file in the constructor and then added to the message.Attachments collection. If you have more than one file to attach in the email, you can add them one by one in the message.Attachments collection.
Once the smtpClient.Send(message) line will execute successfully, an email will be sent and success message will be written on the page. In case any error, it will go to the catch block and write the actual error with complete description in the label.
I hope this article will be useful for Sathya who has asked this question in the Forums thread as well as others who are looking for sending email in ASP.NET with attachment.
with GMAIL you just modify some parameter
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 200000
};
reference : http://www.dotnetfunda.com/articles/article1106-sending-email-with-attachment-in-aspnet.aspx
No comments:
Post a Comment