0
votes

I can't seem to get this to work. I'm trying to make something to send email from a desktop application, but I keep getting the errors

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. l9sm2010572yhb.26 - gsmtp"

When using 587 for the port, and I get this

A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The operation has timed out.

When using 465 as the port.

Here's the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void genuineButton1_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void fusionButton1_Click(object sender, EventArgs e)
        {
            String Sender = textBox1.Text;
            String Pass = textBox2.Text;
            String Recepient = textBox3.Text;
            String Subject = textBox4.Text;
            String Body = richTextBox1.Text;
            MailMessage message = new MailMessage();
            message.From = new MailAddress(String.Copy(Sender));
            message.To.Add(new MailAddress(String.Copy(Recepient)));
            message.Subject = String.Copy(Subject);
            message.Body = String.Copy(Body);

            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.Port = 465;
            client.Send(message);
            System.Net.NetworkCredential netCre = new System.Net.NetworkCredential(String.Copy(Sender), String.Copy(Pass));
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
                   

Any Ideas?

1

1 Answers

1
votes

Add two more libraries

using system.Net; using system.Net.Mine;

And If there is no problem with your SMTP script, but you still got the message message mentioned above, it should because Gmail blocked the authentication from our server as it detected that it is the first time you login to your Gmail account from another Country or Location. You will need to Login to gmail security center to approve the authntication. Once you approved it ,

please wait a few minutes then sending email from script again. Here are the steps to approve the "Unusual activity alerts" from gmail security center.

a) go to gmail security center via this link blow or Google search for "gmail security" and login with your gmail account https://accounts.google.com/ServiceLogin?elo=1

b) next to "security" / "Recent activity" , click to "view all events"

c) You will able to see "Unusual Activity" , it will show all unusual activity events, select related event and approval it via click " Yes, That was me!"