Problem sending email

I’m trying to send an email through Unity to/from gmail … but I keep getting errors.

I’ve tried Port 25, 587 & 465, to no avail; I’ve tried EnableSsl = true and false, to no avail. Here’s the code:

        string fromaddr = myfromemail@gmail.com";
    string toaddr = "mytoemail@gmail.com";
        string password = "mypassword";
        System.DateTime theTime = System.DateTime.Now;
        int Month = theTime.Month;
        int Year = theTime.Year;
    string MyMonth = "";
    if (Month ==  1) MyMonth = "January";
    if (Month ==  2) MyMonth = "February";
    if (Month ==  3) MyMonth = "March";
    if (Month ==  4) MyMonth = "April";
    if (Month ==  5) MyMonth = "May";
    if (Month ==  6) MyMonth = "June";
    if (Month ==  7) MyMonth = "July";
    if (Month ==  8) MyMonth = "August";
    if (Month ==  9) MyMonth = "September";
    if (Month == 10) MyMonth = "October";
    if (Month == 11) MyMonth = "November";
    if (Month == 12) MyMonth = "December";
       
        MailMessage msg = new MailMessage();
        msg.Subject = "Autopay report for " + MyMonth + " " + Year;       
    msg.From = new MailAddress("<myfromemail@gmail.com>", "Name of from", System.Text.Encoding.UTF8);
    msg.IsBodyHtml = true;
        msg.Body = "Here is the Autopay report for this month.

--
Blah blah blah";
        msg.BodyEncoding = System.Text.Encoding.UTF8;
        msg.SubjectEncoding = System.Text.Encoding.UTF8;
        msg.Attachments.Add(new Attachment("C:\\... path to file ...\File.png"));
        msg.To.Add(toaddr);       
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = true;
        NetworkCredential nc = new NetworkCredential(fromaddr, password);
        smtp.Credentials = nc;
        smtp.Send(msg);

I’ve been pulling my hair out trying to fix this.

But what error?

1 Like
string MyMonth = "";
   if (Month ==  1) MyMonth = "January";
   if (Month ==  2) MyMonth = "February";
   if (Month ==  3) MyMonth = "March";
   if (Month ==  4) MyMonth = "April";
   if (Month ==  5) MyMonth = "May";
   if (Month ==  6) MyMonth = "June";
   if (Month ==  7) MyMonth = "July";
   if (Month ==  8) MyMonth = "August";
   if (Month ==  9) MyMonth = "September";
   if (Month == 10) MyMonth = "October";
   if (Month == 11) MyMonth = "November";
   if (Month == 12) MyMonth = "December";

Arrays are a great thing. Same goes for DateTime.ToString() with format specified.

SmtpException: Server does not support secure connections.
I’ve also gotten SmtpException: 535 No SMTP server defined. Use real server address instead of 127.0.0.1 in your account.