Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
MailMessage obj = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
if (Fileupload1.HasFile)
{
    String FileName = Path.GetFileName(Fileupload1.PostedFile.FileName);
    Fileupload1.PostedFile.SaveAs(Server.MapPath("~\\File\\" + FileName.Trim()));
Fileupload1.PostedFile.SaveAs(Server.MapPath("~\\File\\" + FileName.Trim()));
    string path = "~\\File\\" + FileName;
    lblUpload.Text = "file is uploaded";
  Attachment mail1 = new Attachment(Server.MapPath("~\\File\\" + FileName.Trim())); 
      obj.Attachments.Add(mail1);
}
else {
    lblUpload.Text = "file not uploaded";
}

SmtpClient smt = new SmtpClient("smtp.gmail.com",465 );

smt.EnableSsl = true;
 smt.Timeout = 10000;
smt.UseDefaultCredentials = false;
smt.Credentials = new System.Net.NetworkCredential(txtFrom.Text, txtPwd.Text, "www.gmail.com");

 smt.DeliveryMethod = SmtpDeliveryMethod.Network;
        obj.BodyEncoding = UTF8Encoding.UTF8;
        smt.Credentials = CredentialCache.DefaultNetworkCredentials;

try
{
    smt.Send(obj);
}
catch(Exception e1) // The operation has timed out.
{
    throw e1;
}
Posted
Updated 22-May-14 3:14am
v4
Comments
[no name] 22-May-14 7:43am    
It looks like you are using the wrong port.
Member 10532069 22-May-14 8:31am    
HI! I get These port no: from Below Link
http://www.projectpier.org/node/325

1 solution

use full path when you attaching
C#
Attachment mail1 = new Attachment(Server.MapPath("~\\File\\" + FileName.Trim());

And check these answers for sending mails using gmail
http://stackoverflow.com/questions/1311749/c-sharp-smtpclient-class-not-able-to-send-email-using-gmail[^]
 
Share this answer
 
v2
Comments
Member 10532069 22-May-14 9:18am    
I made smpt Network Credential to default Network Credential but its giving an error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

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