Link to home
Start Free TrialLog in
Avatar of Aanvik
Aanvik

asked on

C# Mail

I have a very small C# windows application and I would like to send email via this exe... Now I see some code template using System.web.mail to send emails... which is pretty simple..
My question is can I do it without SMTP... or do we have any free SMTP available... like from Yahoo or gmail?
Avatar of VincentSG
VincentSG
Flag of Singapore image

Hi!

You can use email host such as gmail to send out your emails.

Look at the following tutorial:
http://www.shabdar.org/send-email-using-gmail-account-asp-net-csharp.html
Avatar of Subrat (C++ windows/Linux)
Hi,
I am having the complete code. If u want I'll send you but that contains some personal informations, So I need to modify that:)When ever I'll be freed, I'll do that. For the time you try to modify the below code to achieve your goal. If not succeed, let me know I'll send u the complete code.
 mail sending code is here with some additional things...........Hope u can easily understand and modify accordingly.

 
String dir_attach = Directory_Name.Text;
            if (dir_attach == "" || dir_attach == " ")
            {
                MessageBox.Show("Plz enter directory name");
                return;
            }
            DirectoryInfo dir = new DirectoryInfo(Directory_Name.Text);
            if (!dir.Exists)
            {
                MessageBox.Show("Plz enter valid directory name");
                return;
            }
            //############################ Validating data ########################

            //###########FROM#######################################################
            //create the mail message
            MailMessage mail = new MailMessage();
            NetworkCredential cred = new NetworkCredential(usr_name.Text, password.Text);
            //set the addresses
            mail.From = new MailAddress(usr_name.Text);

//####################################Adding To#########################################
            try{
              mail.To.Clear();
            } catch(Exception clearexp){
                MessageBox.Show(clearexp.Message);
            }
                            try
                            {
                                mail.To.Add(to.Text);
                            }
                            catch (Exception ee)
                            {
                                MessageBox.Show(ee.Message);
                            }
                       
            //set the content
            mail.Subject = subject.Text;
            mail.Body = message.Text;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       String file_attach = dir.FullName + "\\" + enum_hash.Value.ToString();    // U need to modify it.
            if (File.Exists(file_attach))
                {
                    mail.Attachments.Clear();
                    try
                    {
                        mail.Attachments.Add(new Attachment(file_attach));
                    }
                    catch (Exception oe)
                    {
                        MessageBox.Show(oe.StackTrace);
                    }
                }
                else
                {
                    MessageBox.Show("Coudn't find file in specified path" + file_attach);
                }
 
SmtpClient smtp = new SmtpClient(host_name.Text, Convert.ToInt32(port_no.Text));  // Hope u r understanding what r host_name.txt and port_no.txt( Else replace with suitable values)
                // smtp.EnableSsl
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = cred;
                try
                {
                    smtp.Send(mail);
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Message not Sent due to error:\n" + ee.Message);
                }
}

yOU NEED TO USE
using System.Net.Mail;
using System.Net;
using System.IO;
         
Google Mail SMTP works good with System.web.mail but if you have NAT in your network also see this post: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/77f45c5f-76be-400c-a529-a1e49d6d8e62/
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Host name = smtp.gmail.com
port no = 587

Avatar of Aanvik
Aanvik

ASKER

Thx