Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I need to code in c# to send email with attachment.
Can anybody help pls

Thanks
Posted
Comments
TweakBird 7-Feb-11 7:37am    
Where is effort? Show me. or did you googled it.
Manas Bhardwaj 7-Feb-11 9:40am    
any effort?

Try this:
C#
/// <summary>
/// Send an email from XXX
/// </summary>
/// <param name="to">Message to address</param>
/// <param name="body">Text of message to send</param>
/// <param name="subject">Subject line of message</param>
/// <param name="fromAddress">Message from address</param>
/// <param name="fromDisplay">Display neame for "message from address"</param>
/// <param name="credentialUser">User whose credentials are used for message send</param>
/// <param name="credentialPassword">User password used for message send</param>
/// <param name="attachments">Optional attachments for message</param>
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)
    {
    if (to == null)
        {
        to = ConfigurationManager.AppSettings["SMTPDefaultToAddress"];
        }
    string host = ConfigurationManager.AppSettings["SMTPHost"];
    try
        {
        MailMessage mail = new MailMessage();
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress(to));
        mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
        mail.Subject = subject;
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Priority = MailPriority.Normal;
        foreach (MailAttachment ma in attachments)
            {
            mail.Attachments.Add(ma.File);
            }
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
        smtp.Host = host;
        smtp.Send(mail);
        }
    catch (Exception ex)
        {
        StringBuilder sb = new StringBuilder(1024);
        sb.Append("\nTo:" + to);
        sb.Append("\nbody:" + body);
        sb.Append("\nsubject:" + subject);
        sb.Append("\nfromAddress:" + fromAddress);
        sb.Append("\nfromDisplay:" + fromDisplay);
        sb.Append("\ncredentialUser:" + credentialUser);
        sb.Append("\ncredentialPasswordto:" + credentialPassword);
        sb.Append("\nHosting:" + host);
        ErrorLog(sb.ToString(), ex.ToString(), ErrorLogCause.EmailSystem);
        }
    }
 
Share this answer
 
Hi. Aksh@169:rose:

you the use Code basd on below..

Begin define the library file include the top of the .Cs page.
using System.Net.Mail;
using System.Net;


Then you write the code to Click Event..
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("Email@gmail.com", "Domain.Com");

MailMessage mail = new MailMessage();
mail.To.Add("To@address.com");
mail.CC.Add("CC@address.com");
mail.Subject = "Hi : ";// + txtsubject.Text;
mail.Body = "hi how r u..";// txtcomplaint.Text;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.From = new MailAddress("hi@gmail.com", "Domain", System.Text.Encoding.UTF8);
mail.IsBodyHtml = false;
mail.Priority = MailPriority.High;
smtp.Send(mail);




all the best...
 
Share this answer
 
Comments
Aksh@169 7-Feb-11 13:33pm    
Thanks for your kind reply.....
Another link - Sending Mail Using C# via SMTP[^].

You could always search on the internet.
 
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