I'm creating a Windows app and have a form where the user can fill out his/her name and email address, a subject line, and a message, then clicking a Send button to send an email to me about any question or issues they may be experiencing using my app. I'm testing out what I have, but having some issues. I'm using System.Net.Mail for sending the email. First I tried (but including a try-catch):

private void btnSend_Click(object sender, EventArgs e)
{
	MailMessage mail = new MailMessage();

	mail.From = new MailAddress(txtEmailAddress.Text, txtSender.Text);
	mail.To.Add(strMyEmailAddress);

	mail.Subject = txtSubject.Text;
	mail.Body = txtMessage.Text;

	SmtpClient smtp = new SmtpClient("127.0.0.1");

	smtp.Send(mail);
}

This gave me the following error in my catch:

Error: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

Then I tried:

private void btnSend_Click(object sender, EventArgs e)
{
	MailMessage mail = new MailMessage();

	mail.From = new MailAddress(txtEmailAddress.Text, txtSender.Text);
	mail.To.Add(strMyEmailAddress);

	mail.Subject = txtSubject.Text;
	mail.Body = txtMessage.Text;

	SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
	smtp.EnableSsl = true;
	smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
	smtp.Credentials = new NetworkCredential(strMyGmailAddress, strMyGmailPassword);
	smtp.Timeout = 20000;

	smtp.Send(mail);
}

This worked, but in my inbox, the "sender" was my own Gmail address... not my test email address I filled into my winform in the "From" field. I want (need) the sender's email address.

Any ideas on where I went wrong in my first attempt? Or how I can work around my second attempt to get the sender's email address?

Have a look at this link - http://www.daniweb.com/forums/post1128647.html#post1128647

That would be fine on my own computer IF I had XP Pro... but I've got XP Home... and it wouldn't help users that will be installing my app on their own home computers, especially when they could be using any other OS (ME, XP, Vista, Win7). I don't want users to have to adjust any settings on their computer just to get this part of my app to work for them. If there's an SMTP tool that I can include within the install of my app that will make things work, that would probably be ideal. But I'm not sure of any or how to use them.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {

        public static void Main()
        {
            
MailMessage mail = new MailMessage();

 

mail.From = new MailAddress("tjitender@gmail.com","sss");

mail.To.Add("jin_us@yahoo.com");

 

mail.Subject = "txtSubject.Text";

mail.Body = "txtMessage.Text";

 

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);

smtp.EnableSsl = true;

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.Credentials = new NetworkCredential("tjitender@gmail.com", "password");

smtp.Timeout = 20000;

 

smtp.Send(mail);

        }
    }
}

it worked well

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

start quote:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {

        public static void Main()
        {

MailMessage mail = new MailMessage();



mail.From = new MailAddress("tjitender@gmail.com","sss");

mail.To.Add("jin_us@yahoo.com");



mail.Subject = "txtSubject.Text";

mail.Body = "txtMessage.Text";



SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);

smtp.EnableSsl = true;

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.Credentials = new NetworkCredential("tjitender@gmail.com", "password");

smtp.Timeout = 20000;



smtp.Send(mail);

        }
    }
}

it worked well.

Yes, this was my second attempt (basically)... BUT... in my inbox, the "sender" was the gmail server's email address I used for authentication (email address used in the network credentials)... not the actual sender email address (mail.From). So even though the email was sent, it was the wrong sender email address I want going into my inbox.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

unless u dont have your own smtp server gmail does not allow it
ur gmail id should be from otherwise u can spam whole world with gmail smtp lol

unless u dont have your own smtp server gmail does not allow it
ur gmail id should be from otherwise u can spam whole world with gmail smtp lol

Then what I need (obviously) is my own smtp server... and to be able to include whatever smtp server I want to use within the install of my own application when I give it to other users so it'll be on their machines as well. But I don't know of or how to use those smtp servers. I'm trying to find something that will work, but no one seems to understand that or just won't bother telling me about some and how to use them.

Basically, the app I'm creating will have a window form for the user to optionally email me (the creator of the app) with bug reports, etc. So if I need an smtp server on the user's local machine, I need something that can be included in my application's installation that the user will have nothing or little to configure so the email sending part of my app will work for them.

I do this with my applications. I use yahoo small business hosting where I can use their smtp server to send e-mails. It's not free but it's worth a try.

This article has been dead for over six months. Start a new discussion instead.