In this article I will explain how to send mail with multiple attachments in asp.net.
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.
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);
|
<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>
|
using System;
using System.Net.Mail;
using System.Web.Mail;
using MailMessage
= System.Web.Mail.MailMessage;
|
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);
}
|
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 Email
|
|||
|
|
Subscribe by RSS
Subscribe by Email
19 comments :
How can i find smtp server address
Please tell me how can i know the address of SMTP server..
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.
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'.
that problem because of missing using System.Web.Mail; namespace in your code. Please check it once....
Ok Thank you...Please give me the solution for the recent asked query
No sir. I have written that namespace.
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;
Is there any solution for this above problem?????
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'.
Will you plz help me to solve this exception???
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.
Thanx but i had also tried this code also,it still gives the error
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??
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));
http://www.systemwebmail.com/faq/4.3.7.aspx
Thanx for the help...
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);
}
}
check this URL as pasting HTML is not allowed here
http://donyogesh.blogspot.in/2012/07/email-sending-with-attachment.html