Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing below code......When I choose attachment file for sending email then it goes to destination successfully.........But when I was not selected attachment file for sending email Then it display Successfully message..but actually did not got the mail.......How to solve it.......Thanks in advance................
C#
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new                                       NetworkCredential("Username", "Password");
MailMessage msg = new MailMessage();
msg.To.Add(txtto.Text);
msg.From = new MailAddress("@Username");
msg.Subject = txtsubject.Text;
msg.Body = txtmessage.Text;

if (!string.IsNullOrEmpty(txtattachment.Text))
{
  Attachment data = new Attachment(txtattachment.Text);
  msg.Attachments.Add(data);
}
client.Send(msg);

MessageBox.Show("successfully send.....");
Posted
Updated 10-Mar-15 22:23pm
v2

 
Share this answer
 
Comments
[no name] 11-Mar-15 4:35am    
Good Answer Peter +5 :)
Peter Leow 11-Mar-15 4:39am    
Thank you, RajeeshMenoth.
[no name] 11-Mar-15 4:40am    
welcome :)
Attaching document..

C#
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Globalization;

string to = "Toaddress@gmail.com";//To address
string from = "From_adress@gmail.com";//From address
MailMessage message = new MailMessage(from, to);

if (fuAttachment.HasFile)//Attaching document 
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            message .Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
message.Subject = txt_subject.text;
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("yourmail id", "Password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
throw ex;
}


Ref : how to send mail with attachement in asp.net
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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