Click here to Skip to main content
16,019,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to send an email with a file attachment
I Use Gmail SMTP inside asp page
When the experiment and send of computer its's working properly

When lifting on Hosting sends only the message is not sent the attached file

The Code
C#
protected string UserName = "******@gmail.com";
protected string Pass = "*******";
protected string MailRes = "*******@gmail.com";


protected void Button1_Click(object sender, System.EventArgs e)
{

    using (MailMessage mailMessage = new MailMessage())
    {
        string Msgsb =
   "\n Name : " + txt_name.Text.Trim()
   + "\n Address : " + txt_address.Text.Trim()
   + "\n Message : " + txtBody.Text.Trim();

        mailMessage.From = new MailAddress(UserName);
        mailMessage.Subject = txtSubject.Text.Trim();
        mailMessage.Body = Msgsb;
        mailMessage.IsBodyHtml = false;
        mailMessage.To.Add(new MailAddress(MailRes));

        List<httppostedfile> files = (List<httppostedfile>)Session["Files"];
        foreach (HttpPostedFile file in files)
        {
            mailMessage.Attachments.Add(new Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType));
            Label1.Text = Label1.Text + file.FileName;

        }

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = mailMessage.From.Address;
        NetworkCred.Password = Pass;
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mailMessage);

    }
Posted
Updated 17-Jan-13 20:27pm
v3
Comments
bbirajdar 17-Jan-13 5:52am    
What help do you need? Why are you lifting the hosting.. The earlier one was good.
mh20005@hotmail.com 17-Jan-13 7:19am    
from my Computer its work good but from hosting it's not work "the Email came without the attached file".
Nandakishore G N 17-Jan-13 5:52am    
post the code what you have done till now.
P_Dash 20-Jan-13 5:10am    
Okay.
Now tell are all the Files present in Session variable present in your Application ?

1 solution

First off, change your user name - never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know.

Secondly, check where your file resides - C# code is executed on the server, not the client, and the server has no access at all to files located on the client. Such files need to be uploaded to the server before they can be attached.

When you run it in development, the server and client are the same computer, so it looks like it works. When you upload teh site to the server, it can't access the files any more.
 
Share this answer
 
Comments
P_Dash 17-Jan-13 12:42pm    
Yes I also think this is the problem with you.
1st you need to check if that File is present in your Server or not.
Also you you have to Give the proper path if you are attaching the file from your code but not by user Selection.

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