2010-03-31 448 views
0

我正在寫一個程序,通過Gmail發送電子郵件,但我有嚴重的操作超時錯誤。可能的原因是什麼?通過Gmail發送電子郵件

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

ü可以指定發送郵件/添加你得到的異常的確切細節 – 2010-03-31 10:07:50

回答

0

試試這個代碼

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); 
0

使用該代碼使用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 
1

你只需要改變端口587

相關問題