Introduction
This post
will explain you how to sending the mail from the asp.net along with file
attachment
Note:- You
can send even though you company having Gmail access
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
namespace SampleTestingCodes
{
public partial class SendingMailFromAsp
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
//SendMail();
SendingMailByAttachment();
}
protected void
SendingMailByAttachment()
{
// GMAIL Address from where you send the mail
var fromAddress = "lokeshtec@gmail.com";
// any address where the email will be sending
var toAddress = "lannam@technobrainltd.com";
//txtemailid.Text.ToString();
//Password of your gmail address
const string
fromPassword = "***********************************";
// Passing the values and make a email formate to display
string subject = "Attachment";
string
body = "Empty";
//string body = "From: " + txtyourname.Text +
"\n";
//body += "Email: " + txtemailid.Text +
"\n";
//body += "Subject: " + txtsubject.Text +
"\n";
//body += "Question: \n" + txtbody.Text +
"\n";
// smtp settings
var smtp = new
System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
//This the process for attachment the files to sending
//U can use File Up loader control also to upload the file
MailMessage objmailMessage = new MailMessage(fromAddress,
toAddress, subject, body);
string path = Server.MapPath("/")
+ "TimeSheet.docx";
Attachment objAttachment = new Attachment(path);
objmailMessage.Attachments.Add(objAttachment);
smtp.Send(objmailMessage);
}
}
}
No comments:
Post a Comment