*********************************************************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
<title>Send mail with multiple file attachments using Gmail in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<p>To:</p>
<p>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</p>
<p>Subject:</p>
<p>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</p>
<p>Message Body:</p>
<p>
<asp:TextBox ID="txtMsgBody" runat="server" TextMode="MultiLine" Columns="80" Rows="10"></asp:TextBox>
</p>
<p>
Attachment:
</p>
<p id="upload-area">
<input id="File1" type="file" runat="server" size="60" />
</p>
<p>
<a href="#" onclick="addTypeFile()">Attach another file</a>
</p>
<p><asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" /></p>
<span id="Span1" runat="server" />
<script type="text/javascript">
function addTypeFile() {
if (!document.getElementById || !document.createElement)
return false;
var uploadArea = document.getElementById("upload-area");
if (!uploadArea)
return;
var newLine = document.createElement("br");
uploadArea.appendChild(newLine);
var newTypeFile = document.createElement("input");
newTypeFile.type = "file";
newTypeFile.size = "60";
if (!addTypeFile.lastAssignedId)
addTypeFile.lastAssignedId = 100;
newTypeFile.setAttribute("id", "file1" + addTypeFile.lastAssignedId);
newTypeFile.setAttribute("name", "file1:" + addTypeFile.lastAssignedId);
uploadArea.appendChild(newTypeFile);
addTypeFile.lastAssignedId++;
}
</script>
</form>
</body>
</html>
*********************************************************************************
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Web;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
List<Attachment> attachment = new List<Attachment>();
HttpFileCollection uploads = HttpContext.Current.Request.Files;
for (int i = 0; i < uploads.Count; i++)
{
if (uploads[i].FileName.Length == 0)
continue;
attachment.Add(new Attachment(uploads[i].InputStream, uploads[i].FileName));
}
MailSender.SendMail(txtTo.Text, txtSubject.Text, txtMsgBody.Text, attachment);
Span1.InnerHtml = "Mail send successfully." ;
}
catch (Exception ex)
{
Span1.InnerHtml = ex.Message;
}
}
}
********************************************************************************
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
public class MailSender
{
public MailSender() { }
/// <summary>
/// Send mail with attachments from your gmail id.
/// </summary>
/// <param name="to"></param>
/// <param name="subject"></param>
/// <param name="messageBody"></param>
/// <param name="attachment"></param>
/// <returns></returns>
public static bool SendMail(String to, String subject, String messageBody, List<Attachment> attachmentCollection)
{
try
{
NetworkCredential networkCredential = new NetworkCredential("YourEmailId@gmail.com", "YourPassword");
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(networkCredential.UserName);
mailMessage.To.Add(new MailAddress(to));
mailMessage.Subject = subject;
mailMessage.Body = messageBody;
mailMessage.IsBodyHtml = true;
foreach (Attachment attachment in attachmentCollection)
{
mailMessage.Attachments.Add(attachment);
}
mailMessage.Priority = MailPriority.High;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
//smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = networkCredential;
smtpClient.Send(mailMessage);
return true;
}
catch (Exception)
{
return false;
}
}
}
No comments:
Post a Comment