Use below code to Send multiple attachments in Email
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMail.aspx.cs" Inherits="SendMail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
</head>
<body>
<table cellspacing="0" cellpadding="3" width="100%" bgcolor="navy" border="0">
<tr>
<td align="center">
<span class="titl">ASP.NET Email</span>
</td>
</tr>
</table>
<br />
<div align="center">
<asp:Label ID="lblMessage" runat="server" Font-Names="Arial" Width="540" Visible="False"></asp:Label></div>
<br />
<div align="center">
<form id="Form1" name="form2" method="post" enctype="multipart/form-data" runat="server">
<table cellspacing="0" cellpadding="4" bgcolor="navy" border="0">
<tr>
<td>
<table cellspacing="3" cellpadding="4" width="540" bgcolor="white">
<tr>
<td valign="middle" align="right" width="80">
From:
</td>
<td>
<asp:TextBox ID="txtSender" TabIndex="1" runat="server" MaxLength="100" Width="386px"
CssClass="width386" Font-Names="Arial"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="middle" align="right" width="80">
To:
</td>
<td>
<asp:TextBox ID="txtReceiver" TabIndex="1" runat="server" MaxLength="100" Width="386px"
CssClass="width386" Font-Names="Arial"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="middle" align="right">
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" TabIndex="2" runat="server" MaxLength="200" Width="386px"
CssClass="width386" Font-Names="Arial"></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="top" align="right">
Message:
</td>
<td height="84">
<p>
<asp:TextBox ID="txtBody" TabIndex="4" runat="server" Columns="40" Rows="5" TextMode="MultiLine"
Width="451px" CssClass="width386" Font-Names="Arial"></asp:TextBox></p>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" style="margin-left: 0px"
Width="445px" />
</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>
</table>
</td>
</tr>
</table>
</form>
<h1>
</h1>
</div>
</body>
</html>
.ASPX.CS
using System;
using System.Drawing;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
// Create a new blank MailMessage
MailMessage email = new MailMessage();
// Set the properties of the MailMessage to the values on the form
if (rblMailFormat.SelectedItem.Text == "text")
email.IsBodyHtml = false;
else
email.IsBodyHtml = true;
email.From = new MailAddress(txtSender.Text);
email.To.Add(new MailAddress(txtReceiver.Text));
email.Subject = txtSubject.Text;
email.Body = txtBody.Text;
string strFileName = FileUpload1.PostedFile.FileName;
//adding attachements from browse & upload
Attachment attach = new Attachment(strFileName);
// Follow the same trick for multiple attachment
email.Attachments.Add(attach);
//Set the SMTP server
SmtpClient smtp = new SmtpClient("localhost");
// send the email
smtp.Send(email);
// Reset the form
txtSender.Text = "";
txtReceiver.Text = "";
txtSubject.Text = "";
txtBody.Text = "";
// Dispaly a friendly message telling the user
// his email has been sent
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Black;
lblMessage.Text = "Your email has been sent";
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Red;
lblMessage.Text = ex.ToString();
}
}
}
Solution for the QlikView, Biztalk, DotNet and MSBI real time development problems
Search This Blog
Monday, September 26, 2011
How do I change the SMTP port number dynamically in asp.net
Use below method to set dynamic port while sending the email using SMTP
public static void SendMailwithDynamicPort(string strSmtpHost, int strPort, string strMessageBody, string strFrom, string strTo, string strSubject)
{
// Create and configure the smtp client
SmtpClient smtpClient = new SmtpClient();
if (strSmtpHost != null && strSmtpHost.Length > 0)
{
smtpClient.Host = strSmtpHost;
}
smtpClient.Port = strPort; // you can read from config
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage();
mailMessage.Body = strMessageBody;
mailMessage.From = new MailAddress(strFrom);
mailMessage.To.Add(strTo);
mailMessage.Subject = strSubject;
mailMessage.Priority = MailPriority.Normal;//to set the email property
smtpClient.Send(mailMessage);
}
or through Web.Config also we can set port number
public static void SendMailwithDynamicPort(string strSmtpHost, int strPort, string strMessageBody, string strFrom, string strTo, string strSubject)
{
// Create and configure the smtp client
SmtpClient smtpClient = new SmtpClient();
if (strSmtpHost != null && strSmtpHost.Length > 0)
{
smtpClient.Host = strSmtpHost;
}
smtpClient.Port = strPort; // you can read from config
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage();
mailMessage.Body = strMessageBody;
mailMessage.From = new MailAddress(strFrom);
mailMessage.To.Add(strTo);
mailMessage.Subject = strSubject;
mailMessage.Priority = MailPriority.Normal;//to set the email property
smtpClient.Send(mailMessage);
}
or through Web.Config also we can set port number
<configuration> <!-- Add the email settings to the <system.net> element --> <system.net> <mailSettings> <smtp> <network host="relayServerHostname" port="portNumber" userName="username" password="password" /> </smtp> </mailSettings> </system.net> <system.web> ... </system.web> </configuration>
How to get the users IP those who are accessing your website using asp.net
Use below code to get the IP of your website users:
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Client IP...</title>
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<%
string _strClientIP = string.Empty;
string[] _strforwardedIpsList;
// Get exact IP address if proxy available
string _strServerVariable = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(_strServerVariable))
{
_strforwardedIpsList = _strServerVariable.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
_strClientIP = _strforwardedIpsList[_strforwardedIpsList.Length - 1];
}
else
{
// Get Proxy IP address
_strClientIP = HttpContext.Current.Request.UserHostAddress;
if (string.IsNullOrEmpty(_strClientIP))
_strClientIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Response.Write(_strClientIP);
%>
</head>
<body>
<form id="testForm">
</form>
</body>
</html>
Subscribe to:
Posts (Atom)
Popular Posts
-
Qlikview developer, Designer and admin interview questions (Qlikview developer, Designer and admin FAQ’S) 1. Difference between Set ...
-
By default using the LINQ to SQL DataContext design surface (or the SQL Metal command line tool), all parameters are created for a Stored Pr...
-
When building controls dynamically which compose of sub-controls you can sometimes run into an problem where windows throws a “Error creati...
-
.Net Integration with Bill Desk Payment Gateway Introduction: Now-a-days online shopping websites has become very popular, and to h...
-
as is Is Operator is used to check the Compatibility of an Object with a given Type and it returns the result as a Boolean (True or ...