Sending an e-mail

admin

Administrator
Staff member
Hello,

I am trying to create an e-mail application. I have created a simple mail application in win forms. The user will put in the fields and click the send button.

I am not really sure how an e-mail application works. But if send an e-mail to my e-mail account i.e. <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->. I send the e-mail. However, I never receive the e-mail that I send using this mail application. The application works ok, and there are no run-time errors. Is there something I need to do, to be able to send this to my hotmail, yahoo, or gmail e-mail accounts.

The code I have used is below. This has been written in C# Visual Studio 2005.

Thanks in advance


using System.Web.Mail;

private void btnSend_Click(object sender, EventArgs e)
{
try
{
//Construct a new mail message and fill it with information from the form
MailMessage aMessage = new MailMessage();
aMessage.From = txtFrom.Text;
aMessage.To = txtTo.Text;
aMessage.Cc = txtCC.Text;
aMessage.Bcc = txtBCC.Text;
aMessage.Subject = txtSubject.Text;
aMessage.Body = txtMessage.Text;

//If an attachment file is indicated, create it and add it to the message
if (txtAttachment.Text.Length > 0)
{
aMessage.Attachments.Add(new MailAttachment(txtAttachment.Text, MailEncoding.Base64));
}

//Now send the message
SmtpMail.Send(aMessage);

//Indicate that the message has been sent
MessageBox.Show("Message sent to " + txtTo.Text, "E-mail Program", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}hi, did u purposely leave out


SmtpMail.SmtpServer = "your mail server goes here";


if not, you are missing it :PHello,

Thanks for your reply. However, l have added the code that was missed, but still getting a problem. Error Message "The transport failed to connect to server"

my code that l added:

Smtp.MailServer = "smtp.yahoo.com";


Thanks in advance for any sugguestions,

SteveHello

I have just found some useful information. I have configured the IIS smtp virtual server.

In the general settings l have add my IP address.

In the access tab, click rely button and granted access to my IP address.

I have added this code:

Smtp.MailServer = "localhost";

or

Smtp.MailServer = "127.0.0.1";


But l am still getting the same error message "Transport failed to connect to server".

Any ideas on how to solve this.

Many thanks,

SteveHello,

I have added this code:


SmtpMail.SmtpServer.Insert(0, "127.0.0.1");


The program executes without any run-time errors, but l did not recieve any e-mail to my yahoo account.

Any suggustions,

SteveSmtp.MailServer = "localhost";


only works if your box is configured w/ email server. I think you are using someone else's email server right? You need to have their smtp address.

p.s: since you are testing ur system, take off the try/catch block for now, because you are interested the error msg after allHello,

This is an simple application I am developing on my own. It is being run on my laptop. I have configured the IIS and set the properites on the general to be my IP address currently 168.120.50.160. On the access tab l have granted permission for the localhost on the reply and connection.

I made the changes to the SmtpMail.SmtpServer = "mail.hotmail.com";. My listing for sending the code is illistrated below. But still does not work. I am sending the e-mail to my hotmail account <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->, but never gets there.

<code>
MailMessage aMessage = new MailMessage();
aMessage.From = txtFrom.Text;
aMessage.To = txtTo.Text;
aMessage.Cc = txtCC.Text;
aMessage.Bcc = txtBCC.Text;
aMessage.Subject = txtSubject.Text;
aMessage.Body = txtMessage.Text;

//If an attachment file is indicated, create it and add it to the message
if (txtAttachment.Text.Length > 0)
{
aMessage.Attachments.Add(new MailAttachment(txtAttachment.Text, MailEncoding.Base64));
}

SmtpMail.SmtpServer = "mail.hotmail.com";

//Now send the message
SmtpMail.Send(aMessage);

//Indicate that the message has been sent
MessageBox.Show("Message sent to " + txtTo.Text, "E-mail Program", MessageBoxButtons.OK, MessageBoxIcon.Information);
</code>

Is there something I am doing wrong, or something I am not doing.

Thanks in advance,

SteveSmtpMail.SmtpServer = "mail.hotmail.com";


Hi, hotmail and yahoo are public webmail system. Therefore those servers will require you to authenicate yourself first before you can utilize their system to send any emails (meaning...you need to look for ways for you to enter ur username and password, but I never tried before, so I need to dig around first).

Let me give you an example of what I've done. I have 3 servers in my office, 1 w/ exchange server named Xserver, 1 w/ SQL server named SQLServer (yea..very creative) and 1 for file sharing, and develop .Net application named TUX.

TUX, Xserver and SQLServer are within the same network, so DNS understands its own name resolution. In my ASP.Net page, I just use the follow code to refer to my email server:


SmtpMail.SmtpServer = "Xserver";
or
SmtpMail.SmtpServer = "(my xserver's ip address goes here)";


See, I don't use username/password because it's within the same network and Active Directory knows who I am, therefore I auto authenicated myself.

If you coming from the outside world, and try to use the same code, my Xserver won't respond to your request unless you authenicate yourself.

I hope this would help you solid the concept a little more :)Hello,

Thanks for your reply and help.

I thought that you could send e-mails to an another e-mail account regardless if it is yahoo, hotmail, etc. Using the code that l have produced.

I have noticed that the e-mail that l try and send are all in the intepub/mailroot/queue.

Right now I am going to use CDONTS, and try and use that to send the e-mail.

Thanks for your help.

If you have any future suggestions, I would be grateful,

SteveHi,

it make sense that you can't send e-mails to an another e-mail account without some kind of authenication. After all, if anyone can write a couple lines of code and start utilizing public email system such as hotmail/yahoo. Don't u think blocking spam is gonna be a pain?

if I randomly generate a <username>@hotmail.com, and i spam your email...you are not gonna be happy heheh.

I suggest you need to setup a smtp server at your box (or at ur server if you have one) then u can use the code:


SmtpMail.SmtpServer = "localhost";


if ur smtp server happens to utilize hotmail's smtp, by all mean, go for it.

Hope that get u 1 step closer to the solution.Hello Sirpelidor,

Thanks for your help. Looks like I have to setup a mail server to get this working. I have never done that before, so something new to learn.

Thanks,

Steve
 
Back
Top