ASP.NET - Send mail with attached file

Asked By Sudhansu shekhar
09-Feb-12 02:28 AM
 Hi all

  I want to send mail with attached file, file is in folder which is inside our aspx application. My mail is sending but not attaching file. Please help me out.

Thanks.
  Lalit M. replied to Sudhansu shekhar
09-Feb-12 02:37 AM
Hi..
use below sample code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

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

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
      MailMessage mail = new MailMessage();
      mail.To.Add(txtTo.Text);
      //mail.To.Add("amit_jain_online@yahoo.com");
      mail.From = new MailAddress(txtFrom.Text);
      mail.Subject = txtSubject.Text;
      mail.Body = txtMessage.Text;
      mail.IsBodyHtml = true;

      //Attach file using FileUpload Control and put the file in memory stream
      if (FileUpload1.HasFile)
      {
        mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
      }
      SmtpClient smtp = new SmtpClient();
      smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
      smtp.Credentials = new System.Net.NetworkCredential
       ("YourGmailID@gmail.com", "YourGmailPassword");
      //Or your Smtp Email ID and Password
      smtp.EnableSsl = true;
      smtp.Send(mail);

    }
}

more code and article you will get here
-------------
http://www.dotnetcurry.com/ShowArticle.aspx?ID=65
http://www.c-sharpcorner.com/uploadfile/rahul4_saxena/send-email-in-Asp-Net-with-attached-file/
http://www.aspnettutorials.com/tutorials/email/email-attach-aspnet2-csharp.aspx
  dipa ahuja replied to Sudhansu shekhar
09-Feb-12 02:50 AM
using System.Net.Mail;
using System.Net;
 
protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress="xx@gmail.com";
  string GmailId="abc@gmail.com";
  string password="";
  string bodyMsg="helo its testing mail";
 
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = txtSubject.Text;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
  //Attach file using FileUpload Control and put the file in memory stream
 
  if (FileUpload1.HasFile)
  {
    mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
  }
 
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");
  smtp.EnableSsl = true;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
  smtp.Send(mail);
}
 
  smr replied to Sudhansu shekhar
09-Feb-12 02:57 AM
hi

try this

public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
     {       
       try
       {         
         // Create the mail message
         MailMessage objMailMsg = new MailMessage(strFrom, strTo);
          
         objMailMsg.BodyEncoding = Encoding.UTF8;
         objMailMsg.Subject = strSubject;
         objMailMsg.Body = strMsg;
         Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));
         objMailMsg.Attachments.Add(at);
         objMailMsg.Priority = MailPriority.High;
         objMailMsg.IsBodyHtml = true;
 
         //prepare to send mail via SMTP transport
         SmtpClient objSMTPClient = new SmtpClient();
         objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
         objSMTPClient.Send(objMailMsg);
         return true;         
       }
       catch (Exception ex)
       {
         throw ex;
       }
     }


follow
http://forums.asp.net/p/1140099/1831864.aspx
Create New Account
help
object sender, EventArgs e) { try { / / bindIamgeFalse(); / / InsertCartsOnOrderId(); / / pnl123.Visible = true; / / img1.ImageUrl = / / GETMAXID(); string path = Server.MapPath("~ / images / logo.jpg"); LinkedResource logo = new LinkedResource(path); MailMessage message = new MailMessage(); logo.ContentId string fromAddress = "yourname@yoursite.com" ; string toAddress = "newuser@hisdomain.com" ; string contentId = "image1" ; string path = Server.MapPath(@ "images / Logo.jpg" ); / / my logo is placed in images folder MailMessage mailMessage = new MailMessage IsBodyHtml = true ; SmtpClient mailSender = new SmtpClient( "localhost" ); / / use this if you are in the development server mailSender.Send(mailMessage); } Note:SmtpClient mailSender = new SmtpClient(ConfigurationManager.AppSettings[ "MyCustomId" ]); / / use this in the Production Server. I have specified my email server in the web.config file Please refer this link http: / / www.emailarchitect.net / easendmail / sdk string fromAddress = "yourname@yoursite.com" ; string toAddress = "newuser@hisdomain.com" ; string contentId = "image1" ; string path = Server.MapPath(@ "images / Logo.jpg" ); / / my logo is placed in images folder MailMessage mailMessage = new MailMessage IsBodyHtml = true ; SmtpClient mailSender = new SmtpClient( "localhost" ); / / use this if you are in the development server mailSender.Send(mailMessage); } Note:SmtpClient mailSender = new SmtpClient(ConfigurationManager.AppSettings[ "MyCustomId" ]); / / use this in the Production Server. I have specified my email server in the web.config file Please refer this link
Hi this is a mail from .net appl i have used same code. . . . . . "; mailmsg.Priority = MailPriority.Normal; System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("fromMaiId @gmail.com", "mypassword"); client.Port = "587"; client.Host = "smtp.gmail.com"; client.EnableSsl = true; object userstate = mailmsg; client.Send(mailmsg); Go thr these links System.Web.UI.WebControls; using System.Net.Mail; public partial class SendMail : System.Web.UI. Page { protected void Btn_SendMail_Click( object sender, EventArgs e) { MailMessage mailObj = new MailMessage ( txtFrom.Text, txtTo.Text From@online.microsoft.com "); message.Body = "This is the message body"; System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); smtp.Send(message); 1. Add and configure smpt server on your pc through Control Panel -> IIS if you want to send emails from your mailMessage . Subject = subject ; mailMessage . IsBodyHtml = true ; mailMessage . BodyEncoding = Encoding . UTF8 ; mailMessage . Body = body ; mailMessage . Priority = MailPriority . Normal ; / / Smtp configuration for localhost / gmail SmtpClient client = new SmtpClient (); client . Credentials = new NetworkCredential ( "" , "" ); / / enter
html . how can i do this To send email first you have to cinfigure your SMTP server You have to use the namespace System.Net.Mail In the button click write this void btnSend_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("your_email_address@gmail.com"); mail.To.Add("to_address@mfc.ae rows indicates temporary demos< / td> < / tr> " & _ "<tr> <td> * *All statistics are based on the page naming conventions Eg., 22_10_2005_< / td> < / tr> " & _ "<tr> <td> < / td> < / tr> <tr> <td> Regards, < / td Some text < / td> < / tr> < / table> < / body> < / html> "; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); MessageBox.Show("mail Send"); } catch (Exception ex weight:bold;""> pretty colors!!< / span > .< / p > " mm.IsBodyHtml = True '(3) Create the SmtpClient object Dim smtp As New SmtpClient '(4) Send the MailMessage (will use the Web.config settings) smtp.Send(mm) refer this link http: / / www.4guysfromrolla.com / articles / 080206-1.aspx Hi, You from your project. Untitled document Step 1 : Add this in Web.config < system.net > < mailSettings > < smtp > < network host = " smtp.gmail.com " port = " 587 " userName = "" password = "" < / smtp > < / mailSettings > < / system.net > Step 2
ReadReceiptTo); } / / Add the Reply to the mail if (ReplyTo ! = "") { mail.ReplyTo = new MailAddress(ReplyTo); } / / Create smtp client objec to send an email SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings.Get("MailServer").ToString() ); try { smtp.Send(mail); / / SmtpMail.Send(msg); return true; } catch (Exception ex) { return false; } } } Use appropriate SMTP server while sending mail, replace the "Localhost" with your smtp server. MailMessage objEmail = new MailMessage(); objEmail.To = txtTo.Text; objEmail.From = txtFrom.Text; objEmail.Cc = txtCc.Text; objEmail.Subject = "Test Email"; objEmail.Body = txtName.Text + ", " + txtComments.Text; objEmail.Priority = MailPriority.High; / / SmtpMail.SmtpServer = "localhost"; try{ SmtpMail.Send(objEmail); Response.Write("Your Email has been sent
strTo ); objMailMsg . BodyEncoding = Encoding . UTF8 ; objMailMsg . Subject = strSubject ; objMailMsg . Body = strMsg ; Attachment at = new Attachment ( Server . MapPath ( "~ / Uploaded / txt.doc" )); objMailMsg . Attachments . Add ( at ); objMailMsg . Priority = MailPriority . High ; objMailMsg . IsBodyHtml = true ; / / prepare to send mail via SMTP transport SmtpClient objSMTPClient = new SmtpClient (); objSMTPClient . DeliveryMethod = SmtpDeliveryMethod . PickupDirectoryFromIis ; objSMTPClient . Send ( objMailMsg ); return true ; } catch ( Exception ex ) { throw ex ; } } hope it helps . / . For System.Web.UI.HtmlControls; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { } protected void btnSend_Click( object sender, EventArgs e) { MailMessage stream if (FileUpload1.HasFile) { mail.Attachments.Add( new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName)); } SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com" ; / / Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential ( "YourGmailID@gmail