0

I am writing a program that send an email through GMail but I have serious Operation timeout error. What is the likely cause.

class Mailer
{

    MailMessage ms;
    SmtpClient Sc;
    public Mailer()
    {
        Sc = new SmtpClient("smtp.gmail.com");

        //Sc.Credentials = CredentialCache.DefaultNetworkCredentials;
        Sc.EnableSsl = true;
        Sc.Port =465;
        Sc.Timeout = 900000000;
        Sc.DeliveryMethod = SmtpDeliveryMethod.Network;
        Sc.UseDefaultCredentials = false;
        Sc.Credentials = new NetworkCredential("uid", "mypss");


    }
    public void MailTodaysBirthdays(List<Celebrant> TodaysCelebrant)
    {
        int i = TodaysCelebrant.Count();
        foreach (Celebrant cs in TodaysCelebrant)
        {
            //if (IsEmail(cs.EmailAddress.ToString().Trim()))
            //{
           ms = new MailMessage();
           ms.To.Add(cs.EmailAddress);
           ms.From = new MailAddress("uid","Developers",System.Text.Encoding.UTF8);
           ms.Subject = "Happy Birthday ";

           String EmailBody = "Happy Birthday " + cs.FirstName;
           ms.Body = EmailBody;
           ms.Priority = MailPriority.High;

           try
            {
              Sc.Send(ms);
            }
                catch (Exception ex)
                {
                    Sc.Send(ms);
                    BirthdayServices.LogEvent(ex.Message.ToString(),EventLogEntryType.Error);
                }
            //}


        }

    }


    }
1
  • 1
    Can u specify/add the exact details of the exception you are getting Commented Mar 31, 2010 at 10:07

4 Answers 4

1

You just need to change port to 587

Sign up to request clarification or add additional context in comments.

Comments

0

Try this code

    MailMessage mM = new MailMessage();
    mM.From = new MailAddress("[email protected]");
    mM.To.Add("[email protected],[email protected]");
    mM.Subject = subject;
    mM.Body = body;
    mM.IsBodyHtml = true;
    SmtpClient sC = new SmtpClient("smtp.gmail.com");
    sC.Port = 587;
    sC.Credentials = new NetworkCredential("[email protected]", "password");
    sC.EnableSsl = true;
    sC.Send(mM);

Comments

0
Sc.Credentials = new NetworkCredential("[email protected]", "mypss");
Sc.Port = 587;

Comments

0

use this code to send mails using gmail account

Public Sub sendmail(ByVal story As String, ByVal from As String, ByVal Too As String)
    Dim mail As New System.Net.Mail.MailMessage()
    mail.[To].Add([too])
    mail.From = New MailAddress(from, "StoryPan", System.Text.Encoding.UTF8)
    mail.Subject = "Your Friend with ID: " + Session("userlogin").ToString() + "  Sending Story"
    mail.SubjectEncoding = System.Text.Encoding.UTF8
    mail.Body = "StoryPan:<br />" + story
    mail.BodyEncoding = System.Text.Encoding.UTF8
    mail.IsBodyHtml = True
    mail.Priority = MailPriority.High
    Dim client As New SmtpClient()
    client.Credentials = New System.Net.NetworkCredential(from, "panstory")
    client.Port = 587
    client.Host = "smtp.gmail.com"
    client.EnableSsl = True
    Try
        client.Send(mail)
        ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType, "HidePageAdd", "closeMainPopup()", True)
        lblposted.Visible = True
        lblposted.Text = "Email sent successfully."
    Catch ex As Exception
        lblemailerror.Visible = True
        lblemailerror.Text = "Send Email Failed"
    End Try
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.