Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is:

C#
protected void Button1_Click(object sender, EventArgs e)
     {
         //string path = "D:\\Test.txt";
         try
         {
             //FileStream file = null;

             // //Read the file. This appears to be the offending line
             //file = File.OpenRead(path);
             //byte[] Content = new byte[file.Length];
             //file.Read(Content, 0, (int)file.Length);
             //file.Close();
             //file.Dispose();
             //Label1.Text = Content.ToString();

             MailMessage mail = new MailMessage();
             //m.ail.To.Add("anzaman_edi@yahoo.com");
             mail.To.Add("imran3may@gmail.com");
             mail.To.Add("shi01715@yahoo.com");
             //mail.To.Add("zahir@eminence-bd.org");
             mail.From = new MailAddress("shishir64092@gmail.com");
             mail.Subject = "Send Email by asp.net code using google or gmail smtp server";

             string Body = "Hi, I am testing Email function in asp.net" +
                           " using google or gmail smtp server"+
                           " and next time I will send you a document from my .aspx file";
             mail.Body = Body;
             Attachment at = new Attachment("D:\\Text.txt");
             mail.Attachments.Add(at);

             mail.IsBodyHtml = true;

             SmtpClient smtp = new SmtpClient("localhost",25);
             smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
             smtp.Credentials = new System.Net.NetworkCredential
                  ("shishir64092@gmail.com", "abcdef");
             //Or your Smtp Email ID and Password
             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
             smtp.EnableSsl = true;

             smtp.Send(mail);
             Label1.Text = "Mail Send...";
         }
         catch (Exception ex)
         {
             Label1.Text = ex.Message;
         }
     }


Than I got the error message:
Could not find file 'D:\Text.txt'.

Is there any problem in Web.Config file ? Please send me a web.config file setting for solve this problem?
Posted
Updated 5-Jun-12 2:40am
v4

I send with out any problem from my local web app , to my yahoo mail from my gmail!
clean up your config, because I have not set any thing , its default is ok!

but set this to your config maybe helpful for you:
XML
<system.net>
  <mailSettings>
      <smtp deliveryMethod="PickupDirectoryFromIis">
          <network defaultCredentials="true" host="localhost" port="25"/>
      </smtp>
  </mailSettings>
</system.net>

and add this to your code:
C#
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
 
Share this answer
 
Comments
UL UL ALBAB 4-Jun-12 5:19am    
Now, I have got another error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. gh10sm10681682pbc.19
taha bahraminezhad Jooneghani 4-Jun-12 5:29am    
paste your config here!
i'm really confuse, because I don't set any thing and it works just fine!
UL UL ALBAB 5-Jun-12 3:50am    
Thank you... At last I have done it.
UL UL ALBAB 5-Jun-12 3:58am    
Can you help me to learn about Web.Config file codes ? Have you any book to learn about this ?
taha bahraminezhad Jooneghani 5-Jun-12 4:12am    
the msdn web site have a good documentation about config files!
use it

XML
<system.net>
    <mailsettings>
      <smtp deliverymethod="PickupDirectoryFromIis" />
    </mailsettings>
  </system.net>


it is the main line:-
C#
<smtp deliverymethod="PickupDirectoryFromIis" />
 
Share this answer
 
Comments
UL UL ALBAB 4-Jun-12 5:21am    
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. gh10sm10681682pbc.19
Please refer following url,

Send mail using Google Apps/Gmail Account using C#[^]

Hope this may help you.
 
Share this answer
 
Comments
UL UL ALBAB 4-Jun-12 5:21am    
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. gh10sm10681682pbc.19
Hi,

You need to configure mail server on your local computer. if your mail server is not configured then you can't send email through local computer.

See links

Configure mail server on Windows 7

Configure mail server on Linux

Thanks
-Amit
 
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