开发者

Sending Email through Gmail

开发者 https://www.devze.com 2022-12-25 11:16 出处:网络
I am writing a program that send an email through GMail but I have serious Operation timeout error. What is the likely cause.

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开发者_StackOverflow中文版",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);
                }
            //}


        }

    }


    }


You just need to change port to 587


Try this code

    MailMessage mM = new MailMessage();
    mM.From = new MailAddress("from@gmail.com");
    mM.To.Add("to@gmail.com,to@yahoo.co.in");
    mM.Subject = subject;
    mM.Body = body;
    mM.IsBodyHtml = true;
    SmtpClient sC = new SmtpClient("smtp.gmail.com");
    sC.Port = 587;
    sC.Credentials = new NetworkCredential("from@gmail.com", "password");
    sC.EnableSsl = true;
    sC.Send(mM);


Sc.Credentials = new NetworkCredential("uid@gmail.com", "mypss");
Sc.Port = 587;


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号