Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to send mail with multiple attachments in asp.net

Jul 10, 2012
Introduction

In this article I will explain how to send mail with multiple attachments in asp.net.

Description:

In previous post I explained how to send mail with attachment in asp.net. Now I will explain how to send mail with multiple attachments in asp.net. During work with one of the application I got requirement like send mail with multiple attachments in asp.net actually we can implement this concept very easily just by adding few lines of code to how to send mail with attachment in asp.net.

If we want to add multiple attachments to mail we need to write code as shown below

MailMessage Msg = new MailMessage();
Msg.From = txtFrom.Text;
Msg.To = txtTo.Text;
Msg.Subject = txtSubject.Text;
//Attaching files to email
Msg.Attachments.Add(mailAttachment);
Msg.Attachments.Add(new Attachment(fileUpload1.PostedFile.FileName));
Msg.Attachments.Add(new Attachment(fileUpload2.PostedFile.FileName));
Msg.Attachments.Add(new Attachment(fileUpload3.PostedFile.FileName));
Msg.Attachments.Add(new Attachment(fileUpload4.PostedFile.FileName));
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpMail.SmtpServer = "10.120.0.21";
SmtpMail.Send(Msg);
For example check below code


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Send Mail with multiple attachements using asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail with multiple Attachments using asp.net</b>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" /><br />
<asp:FileUpload ID="fileUpload2" runat="server" /><br />
<asp:FileUpload ID="fileUpload3" runat="server" /><br />
<asp:FileUpload ID="fileUpload4" runat="server" /><br />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
After completion of aspx page add following namespaces in codebehind
C# Code


using System;
using System.Net.Mail;
using System.Web.Mail;
using MailMessage = System.Web.Mail.MailMessage;
After that add following code in code behind

protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = "administrator@aspdotnet-suresh.com";
// Recipient e-mail address.
Msg.To = "suresh@aspdotnet-suresh.com";
// Subject of e-mail
Msg.Subject = "Multiple Attachment File";
Msg.Body = "Send Mail with multiple attachements in asp.net";
Msg.Attachments.Add(new Attachment(fileUpload1.PostedFile.FileName));
Msg.Attachments.Add(new Attachment(fileUpload2.PostedFile.FileName));
Msg.Attachments.Add(new Attachment(fileUpload3.PostedFile.FileName));
Msg.Attachments.Add(new Attachment(fileUpload4.PostedFile.FileName));
// your remote SMTP server IP.
SmtpMail.SmtpServer = "10.120.0.21";
SmtpMail.Send(Msg);
}
VB.NET Code

Imports System.Net.Mail
Imports System.Web.Mail

Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim Msg As New MailMessage()
' Sender e-mail address.
Msg.From = "administrator@aspdotnet-suresh.com"
' Recipient e-mail address.
Msg.[To] = "suresh@aspdotnet-suresh.com"
' Subject of e-mail
Msg.Subject = "Multiple Attachment File"
Msg.Body = "Send Mail with multiple attachements in asp.net"
Msg.Attachments.Add(New Attachment(fileUpload1.PostedFile.FileName))
Msg.Attachments.Add(New Attachment(fileUpload2.PostedFile.FileName))
Msg.Attachments.Add(New Attachment(fileUpload3.PostedFile.FileName))
Msg.Attachments.Add(New Attachment(fileUpload4.PostedFile.FileName))
' your remote SMTP server IP.
SmtpMail.SmtpServer = "10.120.0.21"
SmtpMail.Send(Msg)
End Sub
End Class
  

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

24 comments :

Anonymous said...

How can i find smtp server address

Anonymous said...

Please tell me how can i know the address of SMTP server..

Suresh Dasari said...

hi,
smtp server means that is mail server(it contains all the configurations for mail exchange) i think that one maintained by network people in your company you can get that server address from those network people.

Anonymous said...

Please give me the solution for following error, I am facing this problem while running above mail sendin code:::

The Error is---

Unable to cast object of type 'System.Net.Mail.Attachment' to type 'System.Web.Mail.MailAttachment'.

Suresh Dasari said...

that problem because of missing using System.Web.Mail; namespace in your code. Please check it once....

Anonymous said...

Ok Thank you...Please give me the solution for the recent asked query

Anonymous said...

No sir. I have written that namespace.

Anonymous said...

I have attached all the following namespaces like...

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net.Mail;
using System.Web.Mail;
using MailMessage = System.Web.Mail.MailMessage;

Anonymous said...

Is there any solution for this above problem?????

Anonymous said...

Hi..this is very helpful coding for me..
but i have exception when i am running this code like

---Unable to cast object of type 'System.Net.Mail.Attachment' to type 'System.Web.Mail.MailAttachment'.

Anonymous said...

Will you plz help me to solve this exception???

Don said...

hello,

instead of using

Msg.Attachments.Add(New Attachment(fileUpload1.PostedFile.FileName));

use

Msg.Attachments.Add(new System.Web.Mail.MailAttachment(fileUpload1.PostedFile.FileName));

for all the 4 attachments

hope this will help you.

Anonymous said...

Thanx but i had also tried this code also,it still gives the error

Anonymous said...

Now the error comes in following statements:

SmtpMail.SmtpServer = "72.167.238.201";
SmtpMail.Send(Msg);


Exception is:
The specified protocol is unknown.

Have you a solution for this problem??

DON said...

This Exception might be shown because of adding attachment incorrectly..

check the following url for information about this error

http://www.systemwebmail.com/faq/4.3.7.aspx


Instead of directly sending file as attachment try to save to server. and after sending email delete the file..

for e.g.

add System.IO; namespace at the top.


if (fileUpload1.PostedFile != null)
{
HttpPostedFile attFile = fileUpload1.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{

string strFileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.PostedFile.SaveAs(Server.MapPath(strFileName));
message.Attachments.Add(new System.Web.Mail.MailAttachment(Server.MapPath(strFileName)));
attach1 = strFileName;
}
}

after the email is sent delete the file

if (attach1 != null)
File.Delete(Server.MapPath(attach1));

Anonymous said...

http://www.systemwebmail.com/faq/4.3.7.aspx

Anonymous said...

Thanx for the help...

Anonymous said...

Still not working properly:
Plz tell me where i am wrong.This is my code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net.Mail;
using System.Web.Mail;
using MailMessage = System.Web.Mail.MailMessage;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = "sweta.gandhi2010@gmail.com";

// Recipient e-mail address.
Msg.To = "sweta.gandhi2010@gmail.com";
// Subject of e-mail
Msg.Subject = "Multiple Attachment File";
Msg.Body = "Send Mail with multiple attachements in asp.net";

Msg.Attachments.Add(new System.Web.Mail.MailAttachment(Server.MapPath(fileUpload1.PostedFile.FileName)));
Msg.Attachments.Add(new System.Web.Mail.MailAttachment(Server.MapPath(fileUpload2.PostedFile.FileName)));
Msg.Attachments.Add(new System.Web.Mail.MailAttachment(Server.MapPath(fileUpload3.PostedFile.FileName)));
Msg.Attachments.Add(new System.Web.Mail.MailAttachment(Server.MapPath(fileUpload4.PostedFile.FileName)));



// your remote SMTP server IP.
SmtpMail.SmtpServer = "72.167.238.201";
SmtpMail.Send(Msg);


//SmtpClient emailClient = new SmtpClient("72.167.238.201",25);
//emailClient.Send(Msg);

}
}

DON said...

check this URL as pasting HTML is not allowed here

http://donyogesh.blogspot.in/2012/07/email-sending-with-attachment.html

shawn said...

but how to select multiple files at same time.
i mean in that case we have taken more uploader.
what if we need to select all the files with single asp:Fileuploader. ?

Anonymous said...

MailMessage mail = new MailMessage("from@gmail.com","to@gmail.com","subject","body");
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,FileUpload1.FileName));
mail.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
mail.Attachments.Add(new Attachment(FileUpload3.PostedFile.InputStream, FileUpload3.FileName));
mail.Attachments.Add(new Attachment(FileUpload4.PostedFile.InputStream, FileUpload4.FileName));

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new NetworkCredential("from@gmail.com", "pass.word");
smtp.EnableSsl = true;
smtp.Send(mail);
Button1.Text = "done";


try this
And thnku suresh, ur post help me a lot to ameliorate my code

NIdhi

Unknown said...

i am trying to mail with multiple attachments but getting "filenotfoundexception was unhandled by user code" error. My code is

Dim smtpServer As New SmtpClient
Dim email As New MailMessage()
smtpServer.UseDefaultCredentials = False
smtpServer.Credentials = New Net.NetworkCredential("ankitdwivedi02@gmail.com", "ANKITrenu")
smtpServer.EnableSsl = True
smtpServer.Host = "smtp.gmail.com"

email = New MailMessage()
email.From = New MailAddress("ankitdwivedi02@gmail.com")
email.To.Add("ankitdwivedi0290@gmail.com")
email.Subject = "registration success"
email.IsBodyHtml = False
email.Body = "body message"
'Dim str As String = FileUpload1.PostedFile.FileName
email.Attachments.Add(New Attachment(FileUpload1.PostedFile.FileName))
email.Attachments.Add(New Attachment(FileUpload2.PostedFile.FileName))
email.Attachments.Add(New Attachment(FileUpload3.PostedFile.FileName))

Try
smtpServer.Send(email)
Catch ex As Exception

Console.Write(ex.Message)

End Try

Please help me out.

SARAT said...

Hi. I need to upload the file to some location. That part was completed. After uploading when I clicking the send button, the mail has to send with the attachment of uploaded file. After sending the mail, the uploaded file has to be deleted(this part also should done on sendbutton_click event.). Please help me.....(ASP.NET)

Unknown said...

Use MailAttachment instead Attachment........

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.