Geeks With Blogs
Shape Without Form He who has a why to live for can bear almost any how

Thought Ayman's snip was pretty cool System.net.mail with Gmail account  I hadn't looked at this part of the namespace.

Here it is with squigles, pretty much the same.

I have been unable to connect the gmail smtp server though and will show my error first...

The Error I get:

Failure sending mail.
System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: An established connection was aborted by the software i
n your host machine
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
   at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object ow
ner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket
6, Int32 timeout)
   at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32
 timeout, GeneralAsyncDelegate asyncCallback)
   at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate
 asyncCallback)
   at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncD
elegate asyncCallback, Int32 creationTimeout)
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

Has System.Net.NetworkCredential changed in VS 2005?

using System;

using System.Collections.Generic;

using System.Text;

using System.Net.Mail;

namespace ConsoleMailSend

{

public static class MailMaker

{

public static void sendmail()

{

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

string msgBody =string.Empty;

System.Net.Mail.SmtpClient smtp =new SmtpClient();

mail.From = new System.Net.Mail.MailAddress(@"ur-email@gmail.com", "c");

mail.To.Add(@"an-email@adomain.com");

mail.Subject = "Subject";

mail.Body = "msgBody";

mail.IsBodyHtml = true ;// This is to enable HTML in your email body

mail.ReplyTo = new MailAddress(@"ur_email@rd.com") ; // This is optional, it allows you to add Reply To email address.

smtp.Host = "smtp.gmail.com";

smtp.Port = 25;

smtp.EnableSsl = true;

smtp.Credentials = new System.Net.NetworkCredential(@"ur-email@gmail.com", "password");

try

{

smtp.Send(mail);

}

catch (System.Exception ex)

{

Console.WriteLine(ex.Message.ToString());

Console.WriteLine(ex.InnerException.ToString());

}

}

}

}

Posted on Thursday, March 9, 2006 2:28 PM C# | Back to top


Comments on this post: Ayman's VB .Net.Mail to C#

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
Do you have a local firewall, antivirus or other filter running that may block this outbound traffic? This error message is typically caused by such things.


Mike Flasko
http://blogs.msdn.com/mflasko
Left by Mike Flasko on Mar 09, 2006 9:01 PM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
Ayman,
Thanks, for the comment. Very possible.
That was my first thought, as I compiled this code while at work and I am offten left in the dark as to what lies between my desktop and the outside world. I had meant to try it from home last night but didn't get a chance. Maybe since I am back at work again I can attempt to conect from the command line.
The other thing that struck me as strange is your code had
Imports System.Net.NetworkCredential
but System.Net.NetworkCredentials is a class not a namespace.
-C
Left by [C.B.W] on Mar 10, 2006 4:55 AM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
In addition - some ISP's block port 25 to all but their own smtp servers in order to limit direct send (and spamming) from their IP ranges.
Left by Hal Lesesne on Mar 10, 2006 7:43 AM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
Try Telnetting to google to see if it's up

- Open a cmd prompt
- type and enter

telnet

- type and enter

open smtp.gmail.com 25

- If you get a blank screen, enter

helo

- This should return

"250 mx.gmail.com at your service"

if you get an error than it's probably being blocked by your network

HTH

-Brian
Left by Brian Foote on Mar 15, 2006 9:47 AM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
Gmail uses a different port, not 25. It is 465 I believe.
Left by MMAS on Oct 06, 2006 8:37 PM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
Thanks. I had the same problem.

Failure sending mail.
System.Net.WebException: Unable to connect to the remote server ---> System.Net.
Sockets.SocketException: An established connection was aborted by the software i
n your host machine

I disabled my firewall and got the problem fixed.

Thanks again
Left by Mirza Irfan Baig on Feb 02, 2007 12:38 AM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
It is working fine, Thanks.
Left by Shiva Prasad on Feb 11, 2008 5:01 AM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
I have a problem with sending messages to email unable to open port 25 and descative firewalls and antivirus and I get

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: It has forced the interruption of an existing connection to the remote host at System.Net.Sockets.Socket . DoConnect (EndPoint endPointSnapshot, SocketAddress socketAddress)

Someone could help me
I appreciate it beforehand
Left by java on Apr 22, 2008 12:16 PM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
Thank you for the information
Left by Berna Arslan on Jan 22, 2009 3:21 AM

# re: Ayman's VB .Net.Mail to C#
Requesting Gravatar...
good informative mail
Left by srinivasan on Mar 06, 2009 10:22 PM

Your comment:
 (will show your gravatar)


Copyright © [C.B.W] | Powered by: GeeksWithBlogs.net