Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I developed a Windows Form Application to send email message to a particular email address. I have written the code for that the form is running properly but while I click the send button it shows an error message as "Server does not supports secure connections". I am unable to find out the reason behind of it. Please somebody help me to complete my project. Thanks
Posted
Comments
braop 24-Feb-13 9:37am    
i think its better for you to post some of your code. it will make it easier for any one to help
Md M Ahmad 25-Feb-13 1:55am    
Sir, the solution to my issue provided by you is not acceptable. Please provide me another solution. Thanks
S. M. Ahasan Habib 25-Feb-13 2:14am    
Please check your smtp server from where mail will send and check the ssl port you are using to send mail. It is better to take any Network guy who can understand the ssl port/configuration issue of smtp server. Though you are not past your code, I hope your code is ok.
Md M Ahmad 25-Feb-13 2:28am    
Yeah Sir my code is ok, even i am giving my code.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace ContextMenuStrip
{
public partial class Form1 : Form
{
MailMessage message;
SmtpClient smtp;
public Form1()
{
InitializeComponent();
}

public static bool IsValidEmail(string strEmailAddress)
{
if (strEmailAddress == null)
{
return false;
}
else
{
return System.Text.RegularExpressions.Regex.IsMatch(strEmailAddress, @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$", RegexOptions.IgnorePatternWhitespace);
}
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
label4.Text = openFileDialog1.FileName;
label4.Visible = true;
linkLabel1.Visible = false;
}
}

private void button1_Click(object sender, EventArgs e)
{
try
{
button1.Enabled = false;
button2.Visible = true;
message = new MailMessage();
if (IsValidEmail(textBox1.Text))
{
message.To.Add(textBox1.Text);
}
if (IsValidEmail(textBox2.Text))
{
message.CC.Add(textBox2.Text);
}
message.Subject = textBox3.Text;
message.From = new MailAddress("mashkoor.niit@gmail.com");
message.Body = textBox4.Text;
if (label4.Text.Length > 0)
{
if (System.IO.File.Exists(label4.Text))
{
message.Attachments.Add(new Attachment(label4.Text));
}
}
smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 587;
//smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("mashkoor.niit@gmail.com","*******");
smtp.SendAsync(message, message.Subject);
//smtp.Send(message);
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
button2.Visible = false;
button1.Enabled = true;
}
}

void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
MessageBox.Show("Email sending cancelled!");
}
else if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else
{
MessageBox.Show("Email sent successfully!");
}
button2.Visible = false;
button1.Enabled = true;
}

private void Form1_Load(object sender, EventArgs e)
{
button2.Visible = false;
}

private void button2_Click(object sender, EventArgs e)
{
smtp.SendAsyncCancel();
MessageBox.Show("Email sending cancelled!");
}
}
}
S. M. Ahasan Habib 25-Feb-13 5:29am    
Please use the following code and try once again.
System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;

Hi,

Try this:
C#
smtpClient.EnableSsl = false; // change 'smtpClient' into the name of your SmtpClient

Hope this helps.
 
Share this answer
 
Comments
Md M Ahmad 25-Feb-13 1:56am    
Sir, the solution to my issue provided by you is not acceptable. Please provide me another solution. Thanks
Please check the thread 1 and thread 2

Please add the following code to your application.

C#
System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;
 
Share this answer
 
v3
Comments
Md M Ahmad 25-Feb-13 1:56am    
Sir, the solution to my issue provided by you is not acceptable. Please provide me another solution. Thanks

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