简体   繁体   English

通过Gmail发送电子邮件

[英]Sending Email through Gmail

I am writing a program that send an email through GMail but I have serious Operation timeout error. 我正在编写一个通过GMail发送电子邮件的程序,但是我遇到严重的操作超时错误。 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);
                }
            //}


        }

    }


    }

您只需要将端口更改为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 使用此代码使用Gmail帐户发送邮件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM