Click here to Skip to main content
15,888,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop a Contact Us page which enables the user to send emails with attachment. I wrote the following code but I don't know why it doesn't work with me. It was working before adding the FileUpload, but after adding it to the form, it did not work.

C#
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;
using System.Net;
using System.Text;


public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MultiView1.SetActiveView(ViewForm);
        }

    }



    protected void btnSend_Click(object sender, ImageClickEventArgs e)
    {
        SmtpClient sc = new SmtpClient("Mail Server");
        StringBuilder sb = new StringBuilder();
        MailMessage msg = null;

        sb.Append("Message from: " + txtName.Text + "\n");
        sb.Append("Email: " + txtEmail.Text + "\n");
        sb.Append("Message   : " + txtMessage.Text + "\n");
        
        //Attach file using FileUpload Control and put the file in memory stream
        if (FileUpload1.HasFile)
        {
            msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }

        try
        {
            msg = new MailMessage(txtEmail.Text,
                "Receiver Email", "Email Title",
                sb.ToString());

            sc.Send(msg);
            MultiView1.SetActiveView(ViewConfirm);
        }
        catch (Exception ex)
        {
            throw ex;
            // something bad happened
            //Response.Write("Something bad happened!");

        }
        finally
        { 

            if (msg != null)
            {
                msg.Dispose();
            }

        }
    }

}
Posted
Updated 25-Oct-11 19:48pm
v2

You have to set a valid mail server address instead of "Mail Server".

Look at this documentation :
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx[^]
 
Share this answer
 
Try this
MailMessage mail = new MailMessage();
mail.To ="me@mycompany.com;him@hiscompany.com;her@hercompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
MailAttachment attachment = new MailAttachment( Server.MapPath( "test.txt" ) ); 
mail.Attachments.Add( attachment );	//add the attachment
SmtpMail.SmtpServer = "smtp.gmail.com";  //your real server goes hereSmtpMail.Send( mail );
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900