Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email through Gmail in C#

Status
Not open for further replies.

tobriain

Programmer
Jul 19, 2006
13
IE
Anyone have any luck sending emails through gmail accounts in C# applications? I can't get it to work. .

Thanks in advance,

Tom.
 
What are you trying at the moment?

Ignorance of certain subjects is a great part of wisdom
 
I have this in the Form_Load method:

System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("sender@gmail.com", "recipient@gmail.com","This is the mail subject", "Just wanted to say Hello");
MyMailMessage.IsBodyHtml = false;
System.Net.NetworkCredential mailAuthentication = new
System.Net.NetworkCredential("sender@gmail.com", "password");
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
mailClient.Send(MyMailMessage);

And I'm getting the following. . . . . .

System.Net.Mail.SmtpException was unhandled
Message="Failure sending mail."
Source="System"
StackTrace:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailTest.Form1.SendMail1() in C:\Documents and Settings\tobriain\Local Settings\Application Data\Temporary Projects\EmailTest\Form1.cs:line 52
at EmailTest.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\tobriain\Local Settings\Application Data\Temporary Projects\EmailTest\Form1.cs:line 24
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EmailTest.Program.Main() in C:\Documents and Settings\tobriain\Local Settings\Application Data\Temporary Projects\EmailTest\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
I have not done this with C# before, but using CDO in VB I have sent emails through my gmail account through port 465 (not 587). I will test it out in a few and see if it still works.

Ignorance of certain subjects is a great part of wisdom
 
Just tried it , port 465 should work.

Ignorance of certain subjects is a great part of wisdom
 
I tried a few different ports, but no luck. I think I have gotten somewhere though, because if I try to set up Outlook Express to send or collect mail from my account, I'm getting an error message. . ..

The connection to the server has failed. Account: 'pop.gmail.com', Server: 'smtp.gmail.com', Protocol: SMTP, Port: 465, Secure(SSL): Yes, Socket Error: 10060, Error Number: 0x800CCC0E

So I think there is something else going on, but don't know what.

Any help is much appreciated,

Tom.


 
Also, your credential should be just your user ID. Like mine is FirstName.LastName

If I try FirstName.LastName(at sign)gmail.com, it blows up on me.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
i have a feeling you have to explicitly set your gmail account to allow access via software other than a browser.

i suggest you get outlook express working first.

the help in gmail itself is pretty comprehensive.



mr s. <;)

 
>>>i have a feeling you have to explicitly set your gmail account to allow access via software other than a browser.


I have never done this, and I have no problems sending through my account.

tobriain - I am confused now, are you trying to send via outlook express, or via SMTP? IIRC (which I may not), Outlook Express is somewhat limited in terms of what can be automated. Having not used outlook express in over 5 years, it's highly likely that things havechanged.

Are you trying to connect through outlook express using username(at)gmail.com, like you are in your code?


Ignorance of certain subjects is a great part of wisdom
 
Hi AlexCuse,

I'm trying to send the mail through SMTP, but I don't think there's any problem with the code, so I'm trying to troubleshoot it. If it'll work in Outlook, at least the port will be correct etc. Alas, it doesn't. I'll have to check if SMTP is blocked for some reason. I'll try it through a different network connection and see will either work.
 
Tom -

There may not be a problem with the code, but have you yet tried changing your login credential from this:

username(at)gmail.com

to this:

username
?

You have not indicated whether or not you did, and what the results were. Trying to log in with this credential will result in a failure to send.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Hi AlexCuse,

Have tried <user>, rather than <user@gmail.com>, but still no go. Will have to try something else.
 
@ All.

Try the next
Code:
       ' Creates a new instance of Gmail's smtp server.
       Dim smtp As New SmtpClient("smtp.gmail.com", 587) ' The smtp address and port number.

       ' Creates the required to and from addresses.
       Dim sendTo As New MailAddress("whoitsgoingto@gmail.com", "senders name here")
       Dim sentFrom As New MailAddress("whoitsbeingsentfrom@gmail.com", "recipiants name goes here")

       ' Creates a new e-mail message
       Dim message As New MailMessage(sentFrom, sendTo)

       ' Adds the subject and body of the e-mail.
       message.Subject = "Subject goes here."
       message.Body = "The body of the e-mail goes here."
       message.IsBodyHtml = True ' Allows for HTML coding in the body.

       ' Adds someone to the Carbon Copy address.
       message.CC.Add(New MailAddress("carboncopyrecipiantsemail@gmail.com", "carbon recipiants name here"))

       ' Provides authentication to use Gmail's SMTP server
       Dim auth As New System.Net.NetworkCredential("youruserid@gmail.com", "your gmail password here")
       smtp.DeliveryMethod = SmtpDeliveryMethod.Network
       smtp.UseDefaultCredentials = False
       smtp.EnableSsl = True
       smtp.Credentials = auth

       Try
           ' Sends e-mail and provides confirmation.
           smtp.Send(message)
           confirm.Text = "Email request was sent."
       Catch ex As Exception
           confirm.Text = "Error: " & ex.Message
       End Try

Have fun
 
Forgot to say.. but it is obvious, that it is in VB.NET.
The transform is pretty simple and you shouldn't have any problem... but in case there is, i'll write this in C#
 
Hey, that doesn't look like C# ;-) It does look much more like the method I was using though.

Thanks TipGiver, that will certainly come in handy. Well, maybe it will. I am pretty sure it will for tobriain :)

Ignorance of certain subjects is a great part of wisdom
 
Hey !

I already use this code and it works fine.
It shouldn't be hard to convert to C#... the assignmnents only need a ; at the end.. etc

If there is any problem (although i dont use C# ;p) let me know
 
TipGiver -

By
Well, maybe it will.
, I just meant that I don't really have a need to send anything via SMTP via a gmail account at the moment (or really anything via SMTP that is not done by SQL Server).

As it is almost exactly what I was using in VB6 with stronger typing I am 99.5% certain that it'll work :)
(although I'm still not sold on port 587, maybe I will try that out this afternoon)

Sorry for any confusion.


Ignorance of certain subjects is a great part of wisdom
 
I'm sold on port 587 - it works. Good man, TipGiver.

Thanks again,

Tom.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top