Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all i had a html text editor control and a file upload control where i need to send a mail directly to "xyz@abc.com" with the html editor control text and attachement file uploaded from file control...

as per my previous question in another form it was explained that i nned to store the file on server and then send mail...but when i asked my client they said that the email xyz@abc.com o which i was sending mail was configured such that anything send to it will store automatically in database.

now please help me in how to start my coding i am not getting how to send the mail with the file adding..please
Posted

create static class and then add this method to in it and just pass the parameters from your page..

create folder attachment in solution explorer and then
uploaded
attachment using file upload control save in that folder..
Email em = new Email();


FileUpload1.SaveAs(Server.MapPath("~/Attachment/") + FileUpload1.FileName);


em.mail("smtp.gmail.com","xyz@gmail.com","password","aa@aa.com","body msg","subject","attachment name");



public void mail(string host, string fromaddress, string password, string toaddress, string body, string subject5, string attach)
    {
        SmtpClient smtp = new SmtpClient();
        MailMessage msg = new MailMessage();
        Attachment ament;
        try
        {
            smtp.Host = host;
            smtp.Credentials = new System.Net.NetworkCredential(fromaddress, password);
           
            msg.From = new MailAddress(fromaddress, fromaddress);
           
            msg.To.Add(toaddress);
           
            if (attach != "")
            {
               
                string path = Server.MapPath("~/Attachment/") + attach;
                ament = new Attachment(path);
                msg.Attachments.Add(ament);
            }
            msg.Subject = subject5;
         
            msg.IsBodyHtml = true;
            msg.Body = body;
            smtp.Send(msg);
        }
        catch (Exception ex)
        {
            Session["Mailerror"] = ex.Message;
        }
    }
 
Share this answer
 
how to start my coding i am not getting how to send the mail with the file adding

Did you bother Googling for this? It's only been documented a couple of hundred thousand times! Google for "C# send email with attachments".
 
Share this answer
 
Comments
spanner21 30-Dec-13 13:28pm    
hello dave here my requirement is that i nned to send the mail with adding file as attachment that is the problem for me as there is no server pathfor me to upload the file....i need to send the mail along with the attachment... i googled it but not found what i am looking for
Dave Kreskowiak 30-Dec-13 13:36pm    
I already told you want to Google for. You're not doing anything special. You don't need a server to upload the file to before sending the attachment. I don't know where you got that idea from, but it's very wrong.
Amey K Bhatkar 30-Dec-13 13:40pm    
sending mail with attachment is very easy but it will take little time to understand if you are doing it first time there are lot help available on Google.
MailMessage mm = new MailMessage("abc@xyz.com")
{
Subject="",
Body =""
} ;
mm.Attachments.Add(new Attachment(memoryStream, "Attacment.pdf"));
SmtpClient mailClient = new SmtpClient(ConfigurationManager.AppSettings["HostName"].ToString(), 25);
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = true;
mailClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SenderId"].ToString(), ConfigurationManager.AppSettings["SenderPwd"].ToString());

mailClient.Send(mm);
 
Share this answer
 
MailMessage mail = new MailMessage();
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filePath);
mail.Attachments.Add(attachment);

enjoy ;)
 
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