Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Salam to all of you.

Below is my code for email sending. The code is working, but I want to send the email with attachment.

Can anyone help me?

C#
MailMessage mailObj = new MailMessage(txtform.Text, txtto.Text, txtsubj.Text, txtbody.Text);
                

                SmtpClient SMTPServer = new SmtpClient("localhost", 25);
                
                SMTPServer.Send(mailObj);
                
                MessageBox.Show("Email succesfully send");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


Thanks in advance!
Posted
Updated 3-Jul-13 23:13pm
v2

 
Share this answer
 
 
Share this answer
 
create attachment and set media Type
// see http://msdn.microsoft.com/de-de/library/system.net.mime.mediatypenames.application.aspx
C#
Attachment data = new Attachment(
                         "PATH_TO_YOUR_FILE", 
                         MediaTypeNames.Application.Octet);

// your path may look like Server.MapPath("~/file.ABC")
C#
message.Attachments.Add(data);


Thanks & regard
Sham :)
 
Share this answer
 
C#
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("Smtp.gmail.com");

mail.From = new MailAddress("evangelist.msc@gmail.com");
mail.To.Add(TextBox2 .Text );
mail.Subject = ("Purchase Order");
mail.Body = (" User Name :" + TextBox1.Text + "Purchase Id:" + TextBox3.Text);
//memorystream zipf = new memorystream();
mail.IsBodyHtml = true;
//System.Net.Mail.Attachment at=new Attachment(textBox6.Text.ToString(), MediaTypeNames.Application.Octet);
//mail.Attachments.Add(at);

//mail.Attachments.Add(new Attachment(textBox6.Text.ToString(), MediaTypeNames.Application.Octet));

SmtpServer.Timeout = 1000000;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("yourmail id@gmail.com", "Your Password");// email address & password
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
MessageBox.Show("mail Send");


use this code
 
Share this answer
 
Try this.....:)
C#
using System.Net.Mail;

C#
MailMessage mail = new MailMessage();
mail.To.Add("reciever's email address");
mail.From = new MailAddress("sender's email address");
mail.Subject = "Conformation of Registration";
string Body = "We are very sorry to Reject your request of Registration in CMS.";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;
smtp.Send(mail);

paste this code in Web.Config
XML
<appsettings>
    <add key="SMTP" value="smtp.gmail.com" />
    <add key="FROMEMAIL" value="SENDER'S EMAIL ID" />
    <add key="FROMPWD" value="PASSWORD" />
  </appsettings>
 
Share this answer
 
v3
Comments
Johnny J. 4-Jul-13 5:10am    
And the attachment, which is what the OP wanted?
hi visit this link

How do I send mail using C#?[^]
 
Share this answer
 
v2

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