Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have C# code that send Email by Gmail very good. I want this script has option for sending attachment too.
How must do that?
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace SendEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            Email();  
        }
        static void Email()
        {
            var fromAddress = new MailAddress("", "");
            var toAddress = new MailAddress("", "");
            const string fromPassword = "";
            const string subject = "";
            const string body = "";
            
           

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                 
                Timeout = 20000
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body,
               

            })
            {
               
                smtp.Send(message);
            }
        }
    }
}

Regards,
Posted
Comments
[no name] 25-Aug-13 11:35am    
Just as a guess.... you would probably want to make use of the Attachments property.

1 solution

Try:
C#
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body,


})
{
    Attachment a = new Attachment("filename.txt");
    message.Attachments.Add(a);
    smtp.Send(message);
}

Hope this helps.
 
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