2012-10-03 43 views
-1

可能重複:
Sending email in .NET through Gmail
How do you send email from a Java app using Gmail?如何使用gmail從asp發送電子郵件?

在這裏,我想從我的ASP應用程序發送電子郵件警報。大多數人表示使用SQL服務器的郵件功能發送電子郵件非常容易。但不幸的是,我目前正在運行SQL Server 2008 Express版本,並且沒有郵件功能。任何人都可以幫助我使用g-mail發送電子郵件。

+2

ASP,ASP.NET不? –

+0

謝謝Bob Kaufman –

+0

它的ASP。你是對的Cuong Le – Dini88

回答

0

嘗試了這一點

  using System.Net.Mail; 
      using System.Net; 
      var fromAddress = new MailAddress("[email protected]", "From Name"); 
      var toAddress = new MailAddress("[email protected]", "To Name"); 
      const string fromPassword = "password"; 
      const string subject = "test"; 
      const string body = "Hey now!!"; 

      var smtp = new SmtpClient 
      { 
       Host = "smtp.gmail.com", 
       Port = 587, 
       EnableSsl = true, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       Credentials = new NetworkCredential(fromAddress.Address, fromPassword), 
       Timeout = 20000 
      }; 
      using (var message = new MailMessage(fromAddress, toAddress) 
      { 
       Subject = subject, 
       Body = body 
      }) 
      { 
       smtp.Send(message); 
      } 
+0

它給我一個錯誤」SMTP服務器需要一個安全連接或者客戶端沒有被認證,服務器響應是:5.5.1需要認證。在「 –

2

這可以幫助你開始:

MailMessage myMessage = new MailMessage(); 
myMessage.Subject = "Subject"; 
myMessage.Body = mailBody; 

myMessage.From = new MailAddress("FromEmailId", "Name"); 
myMessage.To.Add(new MailAddress("ToEmailId", "Name")); 

SmtpClient mySmtpClient = new SmtpClient(); 
mySmtpClient.Send(myMessage); 
+0

由於你的標籤說ASP.NET和C#,這是ASP.NET和C# –

+0

它給出錯誤「SMTP服務器需要安全連接或客戶端未通過身份驗證。服務器響應是:5.7.0首先必須發出STARTTLS命令pw2sm574022pbb.59「 –

1

您可以安裝一個SMTP服務器並運行上述這

[ASP] 
<% 
Set oSmtp = Server.CreateObject("AOSMTP.Mail") 
oSmtp.ServerAddr = "127.0.0.1" 
oSmtp.FromAddr = "[email protected]" 
oSmtp.AddRecipient "name", "[email protected]", 0 

oSmtp.Subject = "your subject" 
oSmtp.BodyText = "your email body" 

If oSmtp.SendMail() = 0 Then 
    Response.Write "OK" 
Else 
    Response.Write oSmtp.GetLastErrDescription() 
End If 
%> 

是ASP *你說的ASP。如果您使用的是asp.net,請使用Rajpurohit的示例代碼,但您需要安裝smtp服務器,或者有權訪問允許遠程連接的服務器(通過中繼或名稱/密碼身份驗證)

1

電子郵件發送代碼: -

SmtpClient client = new SmtpClient(); 
client.UseDefaultCredentials = false; 
// When You use a Gmail Hosting then u You write Host name is smtp.gmail.com. 
client.Host = "smtp.gmail.com"; 
client.Port = 587; 
client.EnableSsl = true; 
client.Credentials = new System.Net. NetworkCredential("[email protected]","Password"); 
MailMessage msg = new MailMessage(); 
      msg.From = new MailAddress("fromAddress"); 
      msg.To.Add("ToAddress"); 
      msg.Subject = "Subject"; 
      msg.IsBodyHtml = true; 
      msg.Priority = MailPriority.Normal; 
      msg.BodyEncoding = System.Text.Encoding.UTF8; 
      msg.body="body"; 
client.Send(message); 

我認爲這將有助於你

相關問題