tags:

views:

794

answers:

5

I have an application that needs to send e-mails. Currently, this is what I am using:

        System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage();
        MyMailMessage.From = new System.Net.Mail.MailAddress(fromemail.Text);
        MyMailMessage.To.Add(toemail.Text);
        MyMailMessage.Subject = subject.Text;
        MyMailMessage.Body = body.Text;
        System.Net.Mail.SmtpClient SMTPServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
        SMTPServer.Port = 587;
        SMTPServer.Credentials = new System.Net.NetworkCredential("email", "password");
        SMTPServer.EnableSsl = true;
        SMTPServer.Send(MyMailMessage);

Is there a simple way to send an e-mail without having to login to a server? Thank you.

A: 

You need an SMTP server that does not require authentication, however to stop it being a SPAM server, it needs some other kind of protection like a firewall.

ck
So I need an open relay server? Is there anywhere I can find one that wouldn't be blacklisted already?
Nate Shoffner
You'd be unlikely to find a reliable one. The best option is to create a new account somehwere and authenticate securely, or to set up your own server.
ck
When you say authenticate securely, do you mean I would hard code the login credentials inside the program? If so, how would I do that without them being exposed to a simple decompiler?
Nate Shoffner
You may use windows authentication too.
jmservera
A client application shouldn't send emails directly using your SMTP server. if it needs to communicate with you then either allow the user to create an email in their own email program (not ideal as webmail is hard to support) or have an interface it can send you info with.
ck
A: 

GMail's SMTP server always requires authentication. You may need to setup your own server to send email without authentication.

Darin Dimitrov
+1  A: 

Configure an SMTP server into your local network (behind a firewall to avoid being a spam source) and use it directly. You can create one in IIS.

jmservera
The problem is, other people will be using it outside of my network.
Nate Shoffner
Then create a wcf service for your application that uses the smtp server internally, so you provide an external interface for your application only, but not an open smtp server.
jmservera
+1  A: 

There are 2 ways to achieve this:

1) Use your local smtp server (e.g. one with IIS on Win2003/2008 server) and write messages to the local pickup queue). This is possible with minimal changes.

2) You need to resolve the target smtp server. For example when you want to send an email to somebody at msn.com, you'll need to get the MX record for msn.com, e.g. something like mx1.msn.com. You can then directly connect to this SMTP server and send your email to the (local) recipient. Note that there are no built-in ways to resolve the MX-host in .NET (in the sense there are no methods on the Dns class to accomplish this) - you need to do it "manually". Also most SMTP hosts will reject connections from home/residential IP addresses.

liggett78
A: 

hey guy's, when i'm trying the same thing, i'm getting following exception. please help!!

Exception: Message = "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.47.109:587"

Code: MailMessage mm = new MailMessage(email, email, "Test Mail", "Please find the document attached."); mm.Attachments.Add(new Attachment(@"F:\IMP.xlsx")); SmtpClient SmtpC = new SmtpClient("smtp.gmail.com"); SmtpC.Port = 587; NetworkCredential networkCredential = new NetworkCredential(email, password); SmtpC.Credentials = networkCredential; SmtpC.EnableSsl = true; SmtpC.Send(mm);

Saurabh Wajpe