Reputation: 15
I want to send mail (gmail to gmail) this is the code with c#:
I now it's easy and exist more tuto.
I test all tuto to send mail but always the same problem
using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace VerificationBlockage
{
class EnvoyerMail
{
public void sendEmail()
{
// Mail message construction
MailMessage mm = new MailMessage("[email protected]", "[email protected]");
// content
mm.Subject = "testing message";
mm.Body = "hello... from .net c# mailmessage";
mm.CC.Add("[email protected]");
// mm.CC.Add("[email protected]");
// mm.Bcc.Add("[email protected]");
// some attachments
//mm.Attachments.Add(new Attachment("c:\\filename.txt"));
// Sending message
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
// ...
// our account credentials
sc.Credentials = new NetworkCredential("[email protected]", "&******&");
sc.EnableSsl = true;
// Catching result
try
{
sc.Send(mm);
MessageBox.Show("Message sent");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
I don't what's the problem. I change the port 25 , 587, 465. the error is le serveur ne prend pas en charge les connexions sécurisées
English Translation:
the server does not support secure connections
Upvotes: 0
Views: 313
Reputation: 687
You canset Gmail to allow such activity (which is blocked for security reasons by default...) here: https://www.google.com/settings/security/lesssecureapps . Good luck!
Upvotes: 0
Reputation: 5808
This suggests the server you are using does not support SSL connections.
Remove the line
sc.EnableSsl = true;
Or Change it to:
sc.EnableSsl = false;
However I am fairly sure Gmail does. Try this:
public string SendGmailMessage(string toAddress, string fromAddress, string ccAddress, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress emailFrom = new MailAddress(fromAddress);
message.From = emailFrom;
message.To.Add(toAddress);
if (!string.IsNullOrEmpty(ccAddress))
{
message.CC.Add(ccAddress);
}
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true; //Add this line
smtpClient.Credentials = new System.Net.NetworkCredential("GMAILUSERNAME", "GMAILPASSWORD");
smtpClient.Send(message);
msg = "Message Sent";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
Upvotes: 1
Reputation: 1378
It's most likely an issue with your firewall. Have you checked it? On your cmd prompt check the following:
Telnet smtp.gmail.com 587
If you do not get a valid response back something is blocking that port.
Upvotes: 0
Reputation: 453
Maybe that depends on the gmail service connected to that port (server side). I've seen some examples using SSL connecting to port 465
I hope this helps.
Upvotes: 0