Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys. I'm currently having troubles with sending an email. Here's the code:

C#
MailMessage mail = new MailMessage();
              SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

              mail.From = new MailAddress("mygoogleaccount@gmail.com");
              mail.To.Add(txtemail.Text);
              mail.Subject = "Registration Successful";
              mail.Body = "Your password is " + pw + ". You can now logon to your account.";

              SmtpServer.Port = 25;
              SmtpServer.Credentials = new System.Net.NetworkCredential("mygoogleaccount@gmail.com", "mypassword");
              SmtpServer.EnableSsl = true;

              SmtpServer.Send(mail);


It can send emails to @live and @yahoo but not on google/gmail. Please help me out... Thanks guys!

[edit]missing quotes added to fix string-format[/edit]
Posted
Updated 15-Dec-12 7:03am
v4
Comments
Richard MacCutchan 15-Dec-12 11:45am    
You need to show what failures you get; no one can guess what is happening when you send to these accounts.
gaga blues 15-Dec-12 11:59am    
The title says it all. I can't send to my gmail account. There is no error what so ever.
Richard MacCutchan 15-Dec-12 12:06pm    
The title may say it all but that does not help us to guess what may be happening.
gaga blues 15-Dec-12 12:10pm    
Well, what is happening is that i don't get any errors after sending an email. It's just that every time i send an email to my google account, I don't receive a mail. It's fine with yahoo and live. I can receive mails successfully there. I tried changing the port but still no luck
Richard MacCutchan 15-Dec-12 12:35pm    
Are you sure it's not getting caught by your spam filters?

Richard MacCutchan mentioned that it might be in the spam. Actually, everything are transferred there so my case is now closed. Thanks Richard MacCutchan!
 
Share this answer
 
Quote:
The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets for managed implementation of Internet protocols that applications can use to send or receive data over the Internet . SMTP protocol is using for sending email from C#. SMTP stands for Simple Mail Transfer Protocol . C# using System.Net.Mail namespace for sending email . We can instantiate SmtpClient class and assign the Host and Port . The default port using SMTP is 25 , but it may vary different Mail Servers .

The following C# source code shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication.

SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer.Port = 587;
SmtpServer.Credentials =
new System.Net.NetworkCredential("username", "password");


using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}


Quote:
You have to provide the necessary information like your gmail username and password etc.
 
Share this answer
 
Comments
gaga blues 15-Dec-12 12:02pm    
I also tried the port 587 but still no luck. :(
Dear gaga blues

SMTP server settings are not the same for every server.
(unforunatly..)

However this info is public.

You should just google SMTP settings gmail (or any other)

I found this brief list of requirements for gmail:
Gmail SMTP server address: smtp.gmail.com
Gmail SMTP user name: Your full Gmail address (e.g. example@gmail.com)
Gmail SMTP password: Your Gmail password
Gmail SMTP port: 465
Gmail SMTP TLS/SSL required: yes

So you want to use this code for gmail:
C#
SmtpServer.Port = 587;
SmtpServer.EnableSsl = true;


Hope this is usefull for you.
Bert
 
Share this answer
 
Comments
Richard MacCutchan 16-Dec-12 3:35am    
Did you not see the comment? It was going to the spam folder.
Nelek 16-Dec-12 4:05am    
Not only the comment. Solution 2 is there as well
Go by this :
C#
var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);


or else :
Send Email in ASP.Net 2.0 - Feed back Form[^]
Sending SMTP mail from ASP.NET and codebehind as C#[^]
hSending Email with attachment in ASP.NET using SMTP Server[^]
 
Share this answer
 
v2
Comments
Richard MacCutchan 16-Dec-12 3:35am    
Did you not see the comment? It was going to the spam folder.
Nelek 16-Dec-12 4:05am    
Not only the comment. Solution 2 is there as well
Richard MacCutchan 16-Dec-12 4:06am    
Indeed, people are either not reading the answers before posting their own, or are just desperate for reputation points.
Nelek 16-Dec-12 4:10am    
I think the second
gaga blues 16-Dec-12 6:34am    
What's going on here? I thought i said Richard MacCutchan is right so I guess there is no need to add more solutions now. Thanks anyway everybody!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900