Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am just trying to generate an email using C#, however, I think I am using the wrong SMTP server, as Gmail times out for some reason. Is there some security configuration required by Google Mail on the From address, or some authentication mechanism that I need to go through, since this is obviously a 'spam' email example?

What would you suggest?

try
           {
               MailMessage m = new MailMessage();
               m.Subject = "test";
               m.From = new MailAddress("president@usa.gov");
               m.To.Add("camilleri.jon@gmail.com");
               SmtpClient smtp = new SmtpClient("smtp.gmail.com");
               smtp.Send(m);
           }
           catch (Exception ex) { Console.WriteLine(ex.Source); }
Posted
Comments
Sergey Alexandrovich Kryukov 19-Nov-13 12:22pm    
It would make a good answer. Why not posting it as a formal answer?
—SA

Having a mail-server address doesn't mean you can use it.

Usually, you need to be authenticated in some manner, perhaps simply making the query from it's domain. Since you're not making the request from the appropriate location (some part of google) you are rejected by the server.

As an experiment, switch the mail server to the server for they system where your client is authenticated. This may work.

 
Share this answer
 
v2
I recently developed a code as part of a contact page in a website which needed to send email here is the code.

C#
var sender = contact.Email;

           var email = new MailMessage();
           email.To.Add(new MailAddress("arnaldo.skywalker@caudal.cu"));
           email.From = new MailAddress("arnaldo@caudal.cu");
           email.Subject = form["Tipo de contacto"] + " de " + sender;
           email.Body = contact.Message;
           email.IsBodyHtml = true;
           email.Priority = MailPriority.Normal;

           var smtp = new SmtpClient
                          {
                              Host = "192.168.145.209",
                              Port = 25,
                              EnableSsl = false,
                              UseDefaultCredentials = false,
                              Credentials = new NetworkCredential("arnaldo@caudal.cu", "lade$23")
                          };

           try
           {
               smtp.Send(email);

               var verificationEmail = new MailMessage();
               verificationEmail.To.Add(new MailAddress(sender));
               verificationEmail.From = new MailAddress("arnaldo@caudal.cu");
               verificationEmail.Subject = "Su " + form["Tipo de contacto"] + " será atendida " + sender;
               verificationEmail.Body = "Atenderemos su " + form["Tipo de contacto"] + " a la mayor brevedad posible";
               verificationEmail.IsBodyHtml = true;
               verificationEmail.Priority = MailPriority.Normal;

               smtp.Send(verificationEmail);

               email.Dispose();
               verificationEmail.Dispose();
               ViewData["Message"] = "Su mensaje ha sido enviado satisfactoriamente";
           }
           catch (Exception)
           {
 
Share this answer
 

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